Skip to content

Commit

Permalink
fix(schema): Do not error for schemas without properties (#2519)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Jan 5, 2022
1 parent 478e046 commit 96fdb47
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/schema/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ export class Schema<S extends JSONSchemaDefinition> {
ajv: Ajv;
validate: AsyncValidateFunction<FromSchema<S>>;
definition: JSONSchema6;
propertyNames: string[];
readonly _type!: FromSchema<S>;

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 <D extends JSONSchemaDefinition> (definition: D) {
const def = definition as JSONSchema6;
const extended = {
Expand Down
30 changes: 30 additions & 0 deletions packages/schema/test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
});

0 comments on commit 96fdb47

Please sign in to comment.