From 7972b2019c079c93865aeac830f5665c8a4f554f Mon Sep 17 00:00:00 2001 From: margaretjgu Date: Wed, 2 Dec 2026 17:10:41 -0500 Subject: [PATCH] fix: accept cat column lists --- src/factory.ts | 13 ++++++++++++- test/factory.test.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/factory.ts b/src/factory.ts index 834f04fe..784e4484 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -599,7 +599,11 @@ 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')) + if ((relaxFields.length > 0 || columnLists.length > 0) && typeof schema['properties'] === 'object') { const props = { ...(schema['properties'] as Record) } for (const f of relaxFields) { if (f.schemaKey in props) { @@ -607,6 +611,13 @@ async function validateAgainstSchema ( props[f.schemaKey] = {} } } + for (const f of columnLists) { + if (f.schemaKey in props) { + props[f.schemaKey] = { + anyOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }], + } + } + } validationSchema = { ...schema, properties: props } } diff --git a/test/factory.test.ts b/test/factory.test.ts index b8d81f0d..4c22a1d5 100644 --- a/test/factory.test.ts +++ b/test/factory.test.ts @@ -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).h, 'id,s,dpr,mb') + for (const value of ['../', '?#']) { + await invokeAsync(cmd, ['--h', value]) + } + assert.equal((received[1] as Record).h, '../') + assert.equal((received[2] as Record).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' },