diff --git a/packages/schema/src/schema.ts b/packages/schema/src/schema.ts index 2e022bbae0..bf58f81f64 100644 --- a/packages/schema/src/schema.ts +++ b/packages/schema/src/schema.ts @@ -12,19 +12,21 @@ export class Schema { ajv: Ajv; validate: AsyncValidateFunction>; definition: JSONSchema6; - propertyNames: string[]; readonly _type!: FromSchema; constructor (definition: S, ajv: Ajv = AJV) { this.ajv = ajv; this.definition = definition as JSONSchema6; - this.propertyNames = Object.keys(this.definition.properties) this.validate = this.ajv.compile({ $async: true, ...this.definition }); } + get propertyNames () { + return Object.keys(this.definition.properties || {}); + } + extend (definition: D) { const def = definition as JSONSchema6; const extended = { diff --git a/packages/schema/test/schema.test.ts b/packages/schema/test/schema.test.ts index 710b8f5111..15a01d2029 100644 --- a/packages/schema/test/schema.test.ts +++ b/packages/schema/test/schema.test.ts @@ -205,4 +205,34 @@ describe('@feathersjs/schema/schema', () => { user: { email: 'hello@feathersjs.com', age: 42 } }); }); + + it('works with oneOf properties (#2508)', async () => { + const oneOfSchema = schema({ + $id: 'schemaA', + oneOf: [ + { + type: 'object', + additionalProperties: false, + required: ['x'], + properties: { + x: { type:'number'} + } + }, + { + type: 'object', + additionalProperties: false, + required: ['y'], + properties: { + y: { type:'number'} + } + } + ] + } as const); + + const res = await oneOfSchema.validate({ + x: '3' + }); + + assert.deepStrictEqual(res, { x: 3 }); + }); });