Skip to content

Commit

Permalink
Include ajv-formats by default (#2453)
Browse files Browse the repository at this point in the history
  • Loading branch information
lehni authored Jul 5, 2023
1 parent 3bd7adb commit 6b5b300
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
21 changes: 9 additions & 12 deletions lib/model/AjvValidator.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
'use strict';

const Ajv = require('ajv');
const addFormats = require('ajv-formats');
const { Validator } = require('./Validator');
const { ValidationErrorType } = require('../model/ValidationError');
const { isObject, once, cloneDeep: lodashCloneDeep, omit } = require('../utils/objectUtils');

const getAjv = once(() => {
try {
return require('ajv');
} catch (err) {
throw new Error('Optional ajv dependency not installed. Please run `npm install ajv --save`');
}
});

class AjvValidator extends Validator {
static init(self, conf) {
super.init(self, conf);
Expand All @@ -20,8 +14,6 @@ class AjvValidator extends Validator {
allErrors: true,
});

const Ajv = getAjv();

// Create a normal Ajv instance.
self.ajv = new Ajv(
Object.assign(
Expand All @@ -43,8 +35,13 @@ class AjvValidator extends Validator {
// A cache for the compiled validator functions.
self.cache = new Map();

conf.onCreateAjv(self.ajv);
conf.onCreateAjv(self.ajvNoDefaults);
const setupAjv = (ajv) => {
addFormats(ajv);
conf.onCreateAjv(ajv);
};

setupAjv(self.ajv);
setupAjv(self.ajvNoDefaults);
}

beforeValidate({ json, model, options, ctx }) {
Expand Down
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"types": "./typings/objection/index.d.ts",
"dependencies": {
"ajv": "^8.6.2",
"ajv-formats": "^2.1.1",
"db-errors": "^0.2.3"
},
"peerDependencies": {
Expand Down

0 comments on commit 6b5b300

Please sign in to comment.