Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4db0f23

Browse files
committedJan 12, 2024
de-duplicate AJV initialisation
1 parent 7fb52bb commit 4db0f23

File tree

2 files changed

+26
-38
lines changed

2 files changed

+26
-38
lines changed
 

‎index.js

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
const merge = require('@fastify/deepmerge')()
66
const clone = require('rfdc')({ proto: true })
77
const { RefResolver } = require('json-schema-ref-resolver')
8-
const Ajv = require('ajv')
9-
const fastUri = require('fast-uri')
10-
const ajvFormats = require('ajv-formats')
118

129
const validate = require('./lib/schema-validator')
1310
const Serializer = require('./lib/serializer')
@@ -185,22 +182,8 @@ function build (schema, options) {
185182
// we cannot use the validator's AJV instance to actually validate the
186183
// whole schema. Instead, create a new AJV instance with the same options,
187184
// add all the referenced schemas, and then validate:
188-
const ajv = new Ajv({
189-
strictSchema: false,
190-
validateSchema: false,
191-
allowUnionTypes: true,
192-
uriResolver: fastUri,
193-
...options.ajv
194-
})
195-
ajvFormats(ajv)
196-
ajv.addKeyword({
197-
keyword: 'fjs_type',
198-
type: 'object',
199-
errors: false,
200-
validate: (type, date) => {
201-
return date instanceof Date
202-
}
203-
})
185+
const ajv = Validator.createAJVInstanceWithOptions(options.ajv)
186+
204187
if (options.schema) {
205188
for (const key in options.schema) {
206189
const schema = options.schema[key]

‎lib/validator.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,7 @@ const clone = require('rfdc')({ proto: true })
77

88
class Validator {
99
constructor (ajvOptions) {
10-
this.ajv = new Ajv({
11-
strictSchema: false,
12-
validateSchema: false,
13-
allowUnionTypes: true,
14-
uriResolver: fastUri,
15-
...ajvOptions
16-
})
17-
18-
ajvFormats(this.ajv)
19-
20-
this.ajv.addKeyword({
21-
keyword: 'fjs_type',
22-
type: 'object',
23-
errors: false,
24-
validate: (type, date) => {
25-
return date instanceof Date
26-
}
27-
})
28-
10+
this.ajv = Validator.createAJVInstanceWithOptions(ajvOptions)
2911
this._ajvSchemas = {}
3012
this._ajvOptions = ajvOptions || {}
3113
}
@@ -82,6 +64,29 @@ class Validator {
8264
}
8365
}
8466

67+
static createAJVInstanceWithOptions (ajvOptions) {
68+
const ajv = new Ajv({
69+
strictSchema: false,
70+
validateSchema: false,
71+
allowUnionTypes: true,
72+
uriResolver: fastUri,
73+
...ajvOptions
74+
})
75+
76+
ajvFormats(ajv)
77+
78+
ajv.addKeyword({
79+
keyword: 'fjs_type',
80+
type: 'object',
81+
errors: false,
82+
validate: (type, date) => {
83+
return date instanceof Date
84+
}
85+
})
86+
87+
return ajv
88+
}
89+
8590
static restoreFromState (state) {
8691
const validator = new Validator(state.ajvOptions)
8792
for (const [id, ajvSchema] of Object.entries(state.ajvSchemas)) {

0 commit comments

Comments
 (0)
Please sign in to comment.