File tree Expand file tree Collapse file tree 2 files changed +26
-38
lines changed Expand file tree Collapse file tree 2 files changed +26
-38
lines changed Original file line number Diff line number Diff line change 5
5
const merge = require ( '@fastify/deepmerge' ) ( )
6
6
const clone = require ( 'rfdc' ) ( { proto : true } )
7
7
const { RefResolver } = require ( 'json-schema-ref-resolver' )
8
- const Ajv = require ( 'ajv' )
9
- const fastUri = require ( 'fast-uri' )
10
- const ajvFormats = require ( 'ajv-formats' )
11
8
12
9
const validate = require ( './lib/schema-validator' )
13
10
const Serializer = require ( './lib/serializer' )
@@ -185,22 +182,8 @@ function build (schema, options) {
185
182
// we cannot use the validator's AJV instance to actually validate the
186
183
// whole schema. Instead, create a new AJV instance with the same options,
187
184
// 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
+
204
187
if ( options . schema ) {
205
188
for ( const key in options . schema ) {
206
189
const schema = options . schema [ key ]
Original file line number Diff line number Diff line change @@ -7,25 +7,7 @@ const clone = require('rfdc')({ proto: true })
7
7
8
8
class Validator {
9
9
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 )
29
11
this . _ajvSchemas = { }
30
12
this . _ajvOptions = ajvOptions || { }
31
13
}
@@ -82,6 +64,29 @@ class Validator {
82
64
}
83
65
}
84
66
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
+
85
90
static restoreFromState ( state ) {
86
91
const validator = new Validator ( state . ajvOptions )
87
92
for ( const [ id , ajvSchema ] of Object . entries ( state . ajvSchemas ) ) {
You can’t perform that action at this time.
0 commit comments