Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,14 +599,25 @@ async function validateAgainstSchema (
merged.sortParsedKeys.has(a.schemaKey) ||
(a.foundIn === 'body' && (a.type === 'object' || a.type === 'array'))
)
if (relaxFields.length > 0 && typeof schema['properties'] === 'object') {
// Cat `h` and `s` are a comma-separated list, and the published enum drops
// the spec aliases (`s`, `dpr`, `mb`). Keep the string and check that it is
// a string or a list of strings.
const columnLists = schemaArgs.filter((a) => a.description.startsWith('Comma-separated list of column names'))
Comment on lines +602 to +605

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another one where the root cause is upstream schema data, not the CLI. A lot of this probably needs to be applied in @elastic/schemas, either via the ES spec or the code generator.

We'd need to ask others on the team if it makes sense to change CatBase.ts in the ES spec to make some of these column types | string unions and add the missing aliases. _cat APIs are kinda weird and special because the intended audience is... CLI users who want ASCII tables. 😆

Or if this has to land as a stopgap, update the PR description to explain why an upstream fix is not possible or that we're blocked on a decision there,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also: selecting fields by description prefix is fragile. Upstream wording isn't stable, and if it changes upstream, this silently stops matching and #839 comes back. Or if an unrelated field ever starts with this text, it loses validation without anyone noticing.

If a CLI-side exception is the only way, maybe key it on schema structure, or as a hard-coded list of APIs or namespaces.

if ((relaxFields.length > 0 || columnLists.length > 0) && typeof schema['properties'] === 'object') {
const props = { ...(schema['properties'] as Record<string, unknown>) }
for (const f of relaxFields) {
if (f.schemaKey in props) {
// Accept any value for these relaxed fields
props[f.schemaKey] = {}
}
}
for (const f of columnLists) {
if (f.schemaKey in props) {
props[f.schemaKey] = {
anyOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
}
}
}
Comment on lines +614 to +620

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This drops all column validation for the matched flags. Replacing the schema with string | string[] means typos like --h idd or --s nmae now pass --dry-run for ml-jobs, ml-datafeeds, ml-data-frame-analytics, etc. Those mistakes only show up as a server error at runtime.

If a stopgap is kept, consider splitting on , and checking items against the enum plus the known aliases, or getting the upstream types fixed so anyOf handles it.

validationSchema = { ...schema, properties: props }
}

Expand Down
41 changes: 41 additions & 0 deletions test/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1897,6 +1897,47 @@ describe('defineCommand', () => {
assert.match(err, /input validation failed/)
})

it('accepts a comma-separated cat column list, including aliases', async () => {
const schema = jsonSchema({
h: {
description: 'Comma-separated list of column names to display.',
anyOf: [
{ enum: ['id', 'state'] },
{ type: 'array', items: { enum: ['id', 'state'] } },
],
'x-found-in': 'query',
},
})
const received: unknown[] = []
const cmd = defineCommand({
name: 'ml-jobs',
description: 'Cat jobs',
input: schema,
handler: (parsed) => { received.push(parsed.input); return {} },
})
await invokeAsync(cmd, ['--h', 'id,s,dpr,mb'])
assert.equal((received[0] as Record<string, unknown>).h, 'id,s,dpr,mb')
for (const value of ['../', '?#']) {
await invokeAsync(cmd, ['--h', value])
}
assert.equal((received[1] as Record<string, unknown>).h, '../')
assert.equal((received[2] as Record<string, unknown>).h, '?#')
})

it('still rejects a value outside a closed enum', async () => {
const schema = jsonSchema({
level: { enum: ['cluster', 'indices', 'shards'], 'x-found-in': 'query' },
})
const cmd = defineCommand({
name: 'health',
description: 'Health',
input: schema,
handler: () => ({}),
})
const err = await captureErrAsync(cmd, ['--level', 's'])
assert.match(err, /input validation failed/)
})

it('accepts array-typed body fields with any valid JSON', async () => {
const schema = jsonSchema({
operations: { type: 'array', 'x-found-in': 'body' },
Expand Down
Loading