Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support $defs and deprecate definitions #2576

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/model/AjvValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ function jsonSchemaWithoutRequired(jsonSchema) {
),
}
: {},
jsonSchema && jsonSchema.$defs && Object.keys(jsonSchema.$defs).length > 0
? {
$defs: Object.assign(
...Object.keys(jsonSchema.$defs).map((prop) => ({
[prop]: jsonSchemaWithoutRequired(jsonSchema.$defs[prop]),
})),
),
}
: {},

jsonSchema.discriminator && jsonSchema.discriminator.propertyName
? { required: [jsonSchema.discriminator.propertyName] }
: {},
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/model/AjvValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ describe('AjvValidator', () => {
anyOf: [{ $ref: '#/definitions/TestRef1' }, { $ref: '#/definitions/TestRef2' }],
};

const schemaBis = {
$defs: {
TestRef1: {
type: 'object',
properties: {
aRequiredProp1: { type: 'string' },
},
required: ['aRequiredProp1'],
},
TestRef2: {
type: 'object',
properties: {
aRequiredProp2: { type: 'string' },
},
required: ['aRequiredProp2'],
},
},
anyOf: [{ $ref: '#/$defs/TestRef1' }, { $ref: '#/$defs/TestRef2' }],
};

const schema2 = {
type: 'object',
discriminator: { propertyName: 'foo' },
Expand Down Expand Up @@ -102,6 +122,15 @@ describe('AjvValidator', () => {
definitions.forEach((d) => expect(d.required).to.be(undefined));
});

it('should remove required fields from $defs', () => {
const validator = new AjvValidator({ onCreateAjv: () => {} });
const validators = validator.getValidator(modelClass('test', schemaBis), schemaBis, true);
const $defs = Object.entries(validators.schema.$defs);

expect($defs.length).to.be(2);
$defs.forEach((d) => expect(d.required).to.be(undefined));
});

it('should not remove required fields if there is a discriminator', () => {
const validator = new AjvValidator({
onCreateAjv: () => {},
Expand Down Expand Up @@ -156,5 +185,23 @@ describe('AjvValidator', () => {
true,
);
});

it('should handle empty $defs', () => {
const emptyDefinitionsSchema = {
type: 'object',
required: ['a'],
$defs: {},
additionalProperties: false,
properties: {
a: { type: 'string' },
},
};
const validator = new AjvValidator({ onCreateAjv: () => {} });
validator.getValidator(
modelClass('test', emptyDefinitionsSchema),
emptyDefinitionsSchema,
true,
);
});
});
});
8 changes: 8 additions & 0 deletions typings/objection/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1869,6 +1869,13 @@ declare namespace Objection {
$schema?: JSONSchemaVersion;
$comment?: string;

/**
* @see https://json-schema.org/draft/2019-09/release-notes
*/
$defs?: {
[key: string]: JSONSchemaDefinition;
};

/**
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
*/
Expand Down Expand Up @@ -1948,6 +1955,7 @@ declare namespace Objection {

/**
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
* @deprecated Rename to $defs
*/
definitions?: {
[key: string]: JSONSchemaDefinition;
Expand Down