diff --git a/doc/release-notes/changelog.md b/doc/release-notes/changelog.md index ba7155172..ee933127d 100644 --- a/doc/release-notes/changelog.md +++ b/doc/release-notes/changelog.md @@ -29,7 +29,7 @@ ### What's new -- Only add AJV formats if they weren't added in user-land already [#2482](https://github.com/Vincit/objection.js/pull/2482) +- Only add Ajv formats if they weren't added in user-land already [#2482](https://github.com/Vincit/objection.js/pull/2482) ## 3.1.0 diff --git a/lib/model/AjvValidator.js b/lib/model/AjvValidator.js index 3083417f7..023c52945 100644 --- a/lib/model/AjvValidator.js +++ b/lib/model/AjvValidator.js @@ -36,8 +36,10 @@ class AjvValidator extends Validator { self.cache = new Map(); const setupAjv = (ajv) => { - conf.onCreateAjv(ajv); - // Only add AJV formats if they weren't added in user-space already + if (conf.onCreateAjv) { + conf.onCreateAjv(ajv); + } + // Only add Ajv formats if they weren't added in user-space already if (!ajv.formats['date-time']) { addFormats(ajv); } @@ -87,7 +89,7 @@ class AjvValidator extends Validator { } getValidator(modelClass, jsonSchema, isPatchObject) { - // Use the AJV custom serializer if provided. + // Use the Ajv custom serializer if provided. const createCacheKey = this.ajvOptions.serialize || JSON.stringify; // Optimization for the common case where jsonSchema is never modified. @@ -103,7 +105,7 @@ class AjvValidator extends Validator { if (!validators) { validators = { // Validator created for the schema object without `required` properties - // using the AJV instance that doesn't set default values. + // using the Ajv instance that doesn't set default values. patchValidator: null, // Validator created for the unmodified schema. diff --git a/lib/model/Model.js b/lib/model/Model.js index 7579e6e2c..5c160eb11 100644 --- a/lib/model/Model.js +++ b/lib/model/Model.js @@ -317,9 +317,6 @@ class Model { static createValidator() { return new AjvValidator({ - onCreateAjv: (ajv) => { - /* Do Nothing by default */ - }, options: { allErrors: true, validateSchema: false, diff --git a/tests/integration/misc/#1718.js b/tests/integration/misc/#1718.js index b341adc50..689691459 100644 --- a/tests/integration/misc/#1718.js +++ b/tests/integration/misc/#1718.js @@ -5,17 +5,14 @@ const { AjvValidator } = require('../../../lib/model/AjvValidator'); const { ValidationError } = require('../../../lib/model/ValidationError'); module.exports = (session) => { - describe('When Ajv verbose is enabled, pass through the data field in exception #1718', () => { - class A extends Model { + describe('Pass through data in exceptions when Ajv verbose option is enabled #1718', () => { + class MyModel extends Model { static get tableName() { - return 'a'; + return 'MyModel'; } static createValidator() { return new AjvValidator({ - onCreateAjv: (ajv) => { - // Here you can modify the `Ajv` instance. - }, options: { allErrors: true, validateSchema: false, @@ -39,22 +36,22 @@ module.exports = (session) => { beforeEach(() => { return session.knex.schema - .dropTableIfExists('a') - .createTable('a', (table) => { + .dropTableIfExists('MyModel') + .createTable('MyModel', (table) => { table.integer('id').primary(); }) .then(() => { - return Promise.all([session.knex('a').insert({ id: 1 })]); + return Promise.all([session.knex('MyModel').insert({ id: 1 })]); }); }); afterEach(() => { - return session.knex.schema.dropTableIfExists('a'); + return session.knex.schema.dropTableIfExists('MyModel'); }); - it('the test', () => { - return A.query(session.knex) - .insert({ id: '2' }) + it('test', () => { + return MyModel.query(session.knex) + .insert({ id: 2 }) .catch((err) => { expect(err).to.be.an(ValidationError); expect(err.data).to.be.an('object'); diff --git a/tests/ts/examples.ts b/tests/ts/examples.ts index eaaeeef1c..c72876c2a 100644 --- a/tests/ts/examples.ts +++ b/tests/ts/examples.ts @@ -124,8 +124,8 @@ class Person extends objection.Model { static createValidator() { return new objection.AjvValidator({ - onCreateAjv(ajvalidator: Ajv) { - // modify ajvalidator + onCreateAjv(ajv: Ajv) { + // modify ajv }, options: { allErrors: false, diff --git a/tests/ts/fixtures/person.ts b/tests/ts/fixtures/person.ts index 80b2e6006..74be64bca 100644 --- a/tests/ts/fixtures/person.ts +++ b/tests/ts/fixtures/person.ts @@ -63,8 +63,8 @@ export class Person extends objection.Model { static createValidator() { return new objection.AjvValidator({ - onCreateAjv(ajvalidator: Ajv) { - // modify ajvalidator + onCreateAjv(ajv: Ajv) { + // modify ajv }, options: { allErrors: false, diff --git a/tests/unit/model/AjvValidator.js b/tests/unit/model/AjvValidator.js index 24a14786c..469309bf9 100644 --- a/tests/unit/model/AjvValidator.js +++ b/tests/unit/model/AjvValidator.js @@ -94,7 +94,7 @@ describe('AjvValidator', () => { }; it('should remove required fields from definitions', () => { - const validator = new AjvValidator({ onCreateAjv: () => {} }); + const validator = new AjvValidator({}); const validators = validator.getValidator(modelClass('test', schema), schema, true); const definitions = Object.entries(validators.schema.definitions); @@ -104,7 +104,6 @@ describe('AjvValidator', () => { it('should not remove required fields if there is a discriminator', () => { const validator = new AjvValidator({ - onCreateAjv: () => {}, options: { discriminator: true, }, @@ -115,15 +114,13 @@ describe('AjvValidator', () => { it('should add ajv formats by default', () => { expect(() => { - const validator = new AjvValidator({ onCreateAjv: () => {} }); + const validator = new AjvValidator({}); validator.getValidator(modelClass('test', schema3), schema3, true); }).to.not.throwException(); }); it('should remove required fields in inner properties', () => { - const validator = new AjvValidator({ - onCreateAjv: () => {}, - }); + const validator = new AjvValidator({}); const validators = validator.getValidator(modelClass('test', schema4), schema4, true); expect(validators.schema.properties.address.properties).to.not.be(undefined); expect(validators.schema.properties.address.required).to.be(undefined); @@ -149,7 +146,7 @@ describe('AjvValidator', () => { a: { type: 'string' }, }, }; - const validator = new AjvValidator({ onCreateAjv: () => {} }); + const validator = new AjvValidator({}); validator.getValidator( modelClass('test', emptyDefinitionsSchema), emptyDefinitionsSchema,