@@ -44,7 +44,8 @@ function mergeLocation (location, key) {
44
44
return {
45
45
schema : location . schema [ key ] ,
46
46
schemaId : location . schemaId ,
47
- jsonPointer : location . jsonPointer + '/' + key
47
+ jsonPointer : location . jsonPointer + '/' + key ,
48
+ isValidated : location . isValidated
48
49
}
49
50
}
50
51
@@ -63,27 +64,29 @@ function resolveRef (location, ref) {
63
64
throw new Error ( `Cannot find reference "${ ref } "` )
64
65
}
65
66
67
+ if ( location . isValidated ) {
68
+ validatorSchemasIds . add ( schemaId )
69
+ }
70
+
66
71
if ( schema . $ref !== undefined ) {
67
72
return resolveRef ( { schema, schemaId, jsonPointer } , schema . $ref )
68
73
}
69
74
70
- return { schema, schemaId, jsonPointer }
75
+ return { schema, schemaId, jsonPointer, isValidated : location . isValidated }
71
76
}
72
77
73
78
const contextFunctionsNamesBySchema = new Map ( )
74
79
75
- let isValidatorUsed = null
76
80
let rootSchemaId = null
77
81
let refResolver = null
78
82
let contextFunctions = null
79
- let mergeAllOfSchemas = null
83
+ let validatorSchemasIds = null
80
84
81
85
function build ( schema , options ) {
82
86
contextFunctionsNamesBySchema . clear ( )
83
87
84
- isValidatorUsed = false
85
88
contextFunctions = [ ]
86
- mergeAllOfSchemas = { }
89
+ validatorSchemasIds = new Set ( )
87
90
options = options || { }
88
91
89
92
refResolver = new RefResolver ( )
@@ -122,7 +125,7 @@ function build (schema, options) {
122
125
}
123
126
}
124
127
125
- const location = { schema, schemaId : rootSchemaId , jsonPointer : '#' }
128
+ const location = { schema, schemaId : rootSchemaId , jsonPointer : '#' , isValidated : false }
126
129
const code = buildValue ( location , 'input' )
127
130
128
131
const contextFunctionCode = `
@@ -138,13 +141,9 @@ function build (schema, options) {
138
141
const serializer = new Serializer ( options )
139
142
const validator = new Validator ( options . ajv )
140
143
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
- }
144
+ for ( const schemaId of validatorSchemasIds ) {
145
+ const schema = refResolver . getSchema ( schemaId )
146
+ validator . addSchema ( schema , schemaId )
148
147
}
149
148
150
149
const dependenciesName = [ 'validator' , 'serializer' , contextFunctionCode ]
@@ -164,6 +163,7 @@ function build (schema, options) {
164
163
165
164
if ( options . mode === 'standalone' ) {
166
165
// lazy load
166
+ const isValidatorUsed = validatorSchemasIds . size > 0
167
167
const buildStandaloneCode = require ( './lib/standalone' )
168
168
return buildStandaloneCode ( options , validator , isValidatorUsed , contextFunctionCode )
169
169
}
@@ -172,11 +172,10 @@ function build (schema, options) {
172
172
const contextFunc = new Function ( 'validator' , 'serializer' , contextFunctionCode )
173
173
const stringifyFunc = contextFunc ( validator , serializer )
174
174
175
- isValidatorUsed = false
176
175
refResolver = null
177
176
rootSchemaId = null
178
177
contextFunctions = null
179
- mergeAllOfSchemas = null
178
+ validatorSchemasIds = null
180
179
contextFunctionsNamesBySchema . clear ( )
181
180
182
181
return stringifyFunc
@@ -477,7 +476,6 @@ function mergeAllOfSchema (location, schema, mergedSchema) {
477
476
delete mergedSchema . allOf
478
477
479
478
mergedSchema . $id = `merged_${ randomUUID ( ) } `
480
- mergeAllOfSchemas [ mergedSchema . $id ] = mergedSchema
481
479
refResolver . addSchema ( mergedSchema )
482
480
location . schemaId = mergedSchema . $id
483
481
location . jsonPointer = '#'
@@ -495,7 +493,8 @@ function buildInnerObject (location) {
495
493
}
496
494
497
495
function addIfThenElse ( location , input ) {
498
- isValidatorUsed = true
496
+ location . isValidated = true
497
+ validatorSchemasIds . add ( location . schemaId )
499
498
500
499
const schema = merge ( { } , location . schema )
501
500
const thenSchema = schema . then
@@ -874,7 +873,8 @@ function buildValue (location, input) {
874
873
let code = ''
875
874
876
875
if ( type === undefined && ( schema . anyOf || schema . oneOf ) ) {
877
- isValidatorUsed = true
876
+ location . isValidated = true
877
+ validatorSchemasIds . add ( location . schemaId )
878
878
879
879
const type = schema . anyOf ? 'anyOf' : 'oneOf'
880
880
const anyOfLocation = mergeLocation ( location , type )
0 commit comments