Skip to content

Commit

Permalink
fix(queryProperty): allow compound oneOf (#2545)
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallswain authored and daffl committed Feb 15, 2022
1 parent 31fbdff commit 3077d2d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/schema/src/query.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { JSONSchema } from 'json-schema-to-ts';

export const queryProperty = <T extends JSONSchema> (definition: T) => ({
oneOf: [
anyOf: [
definition,
{
type: 'object',
Expand Down
45 changes: 45 additions & 0 deletions packages/schema/test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
});
});

0 comments on commit 3077d2d

Please sign in to comment.