From 3077d2d896a38d579ce4d5b530e21ad332bcf221 Mon Sep 17 00:00:00 2001 From: Marshall Thompson Date: Fri, 28 Jan 2022 09:02:41 -0700 Subject: [PATCH] fix(queryProperty): allow compound oneOf (#2545) --- packages/schema/src/query.ts | 2 +- packages/schema/test/schema.test.ts | 45 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/packages/schema/src/query.ts b/packages/schema/src/query.ts index 712a4ed8a0..414cb08245 100644 --- a/packages/schema/src/query.ts +++ b/packages/schema/src/query.ts @@ -1,7 +1,7 @@ import { JSONSchema } from 'json-schema-to-ts'; export const queryProperty = (definition: T) => ({ - oneOf: [ + anyOf: [ definition, { type: 'object', diff --git a/packages/schema/test/schema.test.ts b/packages/schema/test/schema.test.ts index f838159593..14b39a28c0 100644 --- a/packages/schema/test/schema.test.ts +++ b/packages/schema/test/schema.test.ts @@ -252,4 +252,49 @@ describe('@feathersjs/schema/schema', () => { assert.deepStrictEqual(res, { x: 3 }); }); + + it('can handle compound queryProperty', async () => { + const formatsSchema = schema({ + $id: 'compoundQueryProperty', + type: 'object', + required: [], + additionalProperties: false, + properties: { + dobString: queryProperty({ + oneOf: [ + { type: 'string', format: 'date', convert: true }, + { type: 'string', format: 'date-time', convert: true }, + { type: 'object' } + ] + }) + } + } as const, customAjv); + + const validated = await formatsSchema.validate({ + dobString: { $gt: '2025-04-25', $lte: new Date('2027-04-25') } + }); + + assert.ok(validated) + }); + + it('can still fail queryProperty validation', async () => { + const formatsSchema = schema({ + $id: 'compoundQueryPropertyFail', + type: 'object', + required: [], + additionalProperties: false, + properties: { + dobString: queryProperty({ type: 'string' }) + } + } as const, customAjv); + + try { + const validated = await formatsSchema.validate({ + dobString: { $moose: 'test' } + }); + assert(!validated, 'should not have gotten here') + } catch (error: any) { + assert.ok(error.data?.length > 0) + } + }); });