@@ -72,30 +72,30 @@ function resolveRef (location, ref) {
72
72
73
73
const contextFunctionsNamesBySchema = new Map ( )
74
74
75
+ let isValidatorUsed = null
75
76
let rootSchemaId = null
76
77
let refResolver = null
77
- let validator = null
78
78
let contextFunctions = null
79
+ let mergeAllOfSchemas = null
79
80
80
81
function build ( schema , options ) {
81
82
contextFunctionsNamesBySchema . clear ( )
82
83
84
+ isValidatorUsed = false
83
85
contextFunctions = [ ]
86
+ mergeAllOfSchemas = { }
84
87
options = options || { }
85
88
86
89
refResolver = new RefResolver ( )
87
- validator = new Validator ( options . ajv )
88
90
89
91
rootSchemaId = schema . $id || randomUUID ( )
90
92
91
93
isValidSchema ( schema )
92
- validator . addSchema ( schema , rootSchemaId )
93
94
refResolver . addSchema ( schema , rootSchemaId )
94
95
95
96
if ( options . schema ) {
96
97
for ( const key of Object . keys ( options . schema ) ) {
97
98
isValidSchema ( options . schema [ key ] , key )
98
- validator . addSchema ( options . schema [ key ] , key )
99
99
refResolver . addSchema ( options . schema [ key ] , key )
100
100
}
101
101
}
@@ -122,8 +122,6 @@ function build (schema, options) {
122
122
}
123
123
}
124
124
125
- const serializer = new Serializer ( options )
126
-
127
125
const location = { schema, schemaId : rootSchemaId , jsonPointer : '#' }
128
126
const code = buildValue ( location , 'input' )
129
127
@@ -135,7 +133,19 @@ function build (schema, options) {
135
133
}
136
134
${ contextFunctions . join ( '\n' ) }
137
135
return main
138
- `
136
+ `
137
+
138
+ const serializer = new Serializer ( options )
139
+ const validator = new Validator ( options . ajv )
140
+
141
+ if ( isValidatorUsed ) {
142
+ validator . addSchema ( schema , rootSchemaId )
143
+ const externalSchemas = options . schema || { }
144
+ const validatorSchemas = { ...externalSchemas , ...mergeAllOfSchemas }
145
+ for ( const [ schemaKey , schema ] of Object . entries ( validatorSchemas ) ) {
146
+ validator . addSchema ( schema , schemaKey )
147
+ }
148
+ }
139
149
140
150
const dependenciesName = [ 'validator' , 'serializer' , contextFunctionCode ]
141
151
@@ -155,17 +165,18 @@ function build (schema, options) {
155
165
if ( options . mode === 'standalone' ) {
156
166
// lazy load
157
167
const buildStandaloneCode = require ( './lib/standalone' )
158
- return buildStandaloneCode ( options , validator , contextFunctionCode )
168
+ return buildStandaloneCode ( options , validator , isValidatorUsed , contextFunctionCode )
159
169
}
160
170
161
171
/* eslint no-new-func: "off" */
162
172
const contextFunc = new Function ( 'validator' , 'serializer' , contextFunctionCode )
163
173
const stringifyFunc = contextFunc ( validator , serializer )
164
174
175
+ isValidatorUsed = false
165
176
refResolver = null
166
- validator = null
167
177
rootSchemaId = null
168
178
contextFunctions = null
179
+ mergeAllOfSchemas = null
169
180
contextFunctionsNamesBySchema . clear ( )
170
181
171
182
return stringifyFunc
@@ -466,7 +477,7 @@ function mergeAllOfSchema (location, schema, mergedSchema) {
466
477
delete mergedSchema . allOf
467
478
468
479
mergedSchema . $id = `merged_${ randomUUID ( ) } `
469
- validator . addSchema ( mergedSchema )
480
+ mergeAllOfSchemas [ mergedSchema . $id ] = mergedSchema
470
481
refResolver . addSchema ( mergedSchema )
471
482
location . schemaId = mergedSchema . $id
472
483
location . jsonPointer = '#'
@@ -484,6 +495,8 @@ function buildInnerObject (location) {
484
495
}
485
496
486
497
function addIfThenElse ( location , input ) {
498
+ isValidatorUsed = true
499
+
487
500
const schema = merge ( { } , location . schema )
488
501
const thenSchema = schema . then
489
502
const elseSchema = schema . else || { additionalProperties : true }
@@ -861,6 +874,8 @@ function buildValue (location, input) {
861
874
let code = ''
862
875
863
876
if ( type === undefined && ( schema . anyOf || schema . oneOf ) ) {
877
+ isValidatorUsed = true
878
+
864
879
const type = schema . anyOf ? 'anyOf' : 'oneOf'
865
880
const anyOfLocation = mergeLocation ( location , type )
866
881
0 commit comments