Skip to content

Commit 7cfea62

Browse files
fix(queryProperty): allow compound oneOf (#2545)
1 parent 58f03ef commit 7cfea62

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

packages/schema/src/query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { JSONSchema } from 'json-schema-to-ts';
22

33
export const queryProperty = <T extends JSONSchema> (definition: T) => ({
4-
oneOf: [
4+
anyOf: [
55
definition,
66
{
77
type: 'object',

packages/schema/test/schema.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,49 @@ describe('@feathersjs/schema/schema', () => {
252252

253253
assert.deepStrictEqual(res, { x: 3 });
254254
});
255+
256+
it('can handle compound queryProperty', async () => {
257+
const formatsSchema = schema({
258+
$id: 'compoundQueryProperty',
259+
type: 'object',
260+
required: [],
261+
additionalProperties: false,
262+
properties: {
263+
dobString: queryProperty({
264+
oneOf: [
265+
{ type: 'string', format: 'date', convert: true },
266+
{ type: 'string', format: 'date-time', convert: true },
267+
{ type: 'object' }
268+
]
269+
})
270+
}
271+
} as const, customAjv);
272+
273+
const validated = await formatsSchema.validate({
274+
dobString: { $gt: '2025-04-25', $lte: new Date('2027-04-25') }
275+
});
276+
277+
assert.ok(validated)
278+
});
279+
280+
it('can still fail queryProperty validation', async () => {
281+
const formatsSchema = schema({
282+
$id: 'compoundQueryPropertyFail',
283+
type: 'object',
284+
required: [],
285+
additionalProperties: false,
286+
properties: {
287+
dobString: queryProperty({ type: 'string' })
288+
}
289+
} as const, customAjv);
290+
291+
try {
292+
const validated = await formatsSchema.validate({
293+
dobString: { $moose: 'test' }
294+
});
295+
assert(!validated, 'should not have gotten here')
296+
} catch (error: any) {
297+
assert.ok(error.data?.length > 0)
298+
}
299+
});
255300
});

0 commit comments

Comments
 (0)