Skip to content

Commit

Permalink
Make onCreateAjv() callback optional
Browse files Browse the repository at this point in the history
  • Loading branch information
lehni committed Mar 3, 2024
1 parent 293cb42 commit acca19a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 32 deletions.
2 changes: 1 addition & 1 deletion doc/release-notes/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 6 additions & 4 deletions lib/model/AjvValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
3 changes: 0 additions & 3 deletions lib/model/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,6 @@ class Model {

static createValidator() {
return new AjvValidator({
onCreateAjv: (ajv) => {
/* Do Nothing by default */
},
options: {
allErrors: true,
validateSchema: false,
Expand Down
23 changes: 10 additions & 13 deletions tests/integration/misc/#1718.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions tests/ts/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/ts/fixtures/person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 4 additions & 7 deletions tests/unit/model/AjvValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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,
},
Expand All @@ -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);
Expand All @@ -149,7 +146,7 @@ describe('AjvValidator', () => {
a: { type: 'string' },
},
};
const validator = new AjvValidator({ onCreateAjv: () => {} });
const validator = new AjvValidator({});
validator.getValidator(
modelClass('test', emptyDefinitionsSchema),
emptyDefinitionsSchema,
Expand Down

0 comments on commit acca19a

Please sign in to comment.