@@ -40,12 +40,47 @@ function isValidSchema (schema, name) {
40
40
}
41
41
}
42
42
43
- function mergeLocation ( location , key ) {
44
- return {
45
- schema : location . schema [ key ] ,
46
- schemaId : location . schemaId ,
47
- jsonPointer : location . jsonPointer + '/' + key ,
48
- isValidated : location . isValidated
43
+ class Location {
44
+ constructor ( schema , schemaId , jsonPointer = '#' , isValidated = false ) {
45
+ this . schema = schema
46
+ this . schemaId = schemaId
47
+ this . jsonPointer = jsonPointer
48
+ this . isValidated = isValidated
49
+
50
+ this . isMerged = false
51
+ this . mergedSchemaId = null
52
+ this . mergedJsonPointer = null
53
+ }
54
+
55
+ getPropertyLocation ( propertyName ) {
56
+ const propertyLocation = new Location (
57
+ this . schema [ propertyName ] ,
58
+ this . schemaId ,
59
+ this . jsonPointer + '/' + propertyName ,
60
+ this . isValidated
61
+ )
62
+
63
+ if ( this . isMerged ) {
64
+ propertyLocation . addMergedSchema (
65
+ this . mergedSchemaId ,
66
+ this . mergedJsonPointer + '/' + propertyName
67
+ )
68
+ }
69
+
70
+ return propertyLocation
71
+ }
72
+
73
+ getSchemaRef ( ) {
74
+ if ( this . isMerged ) {
75
+ return this . mergedSchemaId + this . mergedJsonPointer
76
+ }
77
+ return this . schemaId + this . jsonPointer
78
+ }
79
+
80
+ addMergedSchema ( schemaId , jsonPointer = '#' ) {
81
+ this . isMerged = true
82
+ this . mergedSchemaId = schemaId
83
+ this . mergedJsonPointer = jsonPointer
49
84
}
50
85
}
51
86
@@ -68,11 +103,12 @@ function resolveRef (location, ref) {
68
103
validatorSchemasIds . add ( schemaId )
69
104
}
70
105
106
+ const newLocation = new Location ( schema , schemaId , jsonPointer , location . isValidated )
71
107
if ( schema . $ref !== undefined ) {
72
- return resolveRef ( { schema , schemaId , jsonPointer } , schema . $ref )
108
+ return resolveRef ( newLocation , schema . $ref )
73
109
}
74
110
75
- return { schema , schemaId , jsonPointer , isValidated : location . isValidated }
111
+ return newLocation
76
112
}
77
113
78
114
const contextFunctionsNamesBySchema = new Map ( )
@@ -125,7 +161,7 @@ function build (schema, options) {
125
161
}
126
162
}
127
163
128
- const location = { schema, schemaId : rootSchemaId , jsonPointer : '#' , isValidated : false }
164
+ const location = new Location ( schema , rootSchemaId )
129
165
const code = buildValue ( location , 'input' )
130
166
131
167
const contextFunctionCode = `
@@ -248,9 +284,9 @@ function addPatternProperties (location) {
248
284
if (properties[keys[i]]) continue
249
285
`
250
286
251
- const patternPropertiesLocation = mergeLocation ( location , 'patternProperties' )
287
+ const patternPropertiesLocation = location . getPropertyLocation ( 'patternProperties' )
252
288
Object . keys ( pp ) . forEach ( ( regex ) => {
253
- let ppLocation = mergeLocation ( patternPropertiesLocation , regex )
289
+ let ppLocation = patternPropertiesLocation . getPropertyLocation ( regex )
254
290
if ( pp [ regex ] . $ref ) {
255
291
ppLocation = resolveRef ( ppLocation , pp [ regex ] . $ref )
256
292
pp [ regex ] = ppLocation . schema
@@ -296,7 +332,7 @@ function additionalProperty (location) {
296
332
return code
297
333
}
298
334
299
- let apLocation = mergeLocation ( location , 'additionalProperties' )
335
+ let apLocation = location . getPropertyLocation ( 'additionalProperties' )
300
336
if ( apLocation . schema . $ref ) {
301
337
apLocation = resolveRef ( apLocation , apLocation . schema . $ref )
302
338
}
@@ -333,9 +369,12 @@ function buildCode (location) {
333
369
334
370
let code = ''
335
371
336
- const propertiesLocation = mergeLocation ( location , 'properties' )
372
+ if ( ! ( location instanceof Location ) ) {
373
+ console . log ( location )
374
+ }
375
+ const propertiesLocation = location . getPropertyLocation ( 'properties' )
337
376
Object . keys ( schema . properties || { } ) . forEach ( ( key ) => {
338
- let propertyLocation = mergeLocation ( propertiesLocation , key )
377
+ let propertyLocation = propertiesLocation . getPropertyLocation ( key )
339
378
if ( propertyLocation . schema . $ref ) {
340
379
propertyLocation = resolveRef ( location , propertyLocation . schema . $ref )
341
380
}
@@ -382,13 +421,13 @@ function buildCode (location) {
382
421
}
383
422
384
423
function mergeAllOfSchema ( location , schema , mergedSchema ) {
385
- const allOfLocation = mergeLocation ( location , 'allOf' )
424
+ const allOfLocation = location . getPropertyLocation ( 'allOf' )
386
425
387
426
for ( let i = 0 ; i < schema . allOf . length ; i ++ ) {
388
427
let allOfSchema = schema . allOf [ i ]
389
428
390
429
if ( allOfSchema . $ref ) {
391
- const allOfSchemaLocation = mergeLocation ( allOfLocation , i )
430
+ const allOfSchemaLocation = allOfLocation . getPropertyLocation ( i )
392
431
allOfSchema = resolveRef ( allOfSchemaLocation , allOfSchema . $ref ) . schema
393
432
}
394
433
@@ -477,8 +516,9 @@ function mergeAllOfSchema (location, schema, mergedSchema) {
477
516
478
517
mergedSchema . $id = `merged_${ randomUUID ( ) } `
479
518
refResolver . addSchema ( mergedSchema )
480
- location . schemaId = mergedSchema . $id
481
- location . jsonPointer = '#'
519
+
520
+ location . schema = mergedSchema
521
+ location . addMergedSchema ( mergedSchema . $id )
482
522
}
483
523
484
524
function buildInnerObject ( location ) {
@@ -494,7 +534,11 @@ function buildInnerObject (location) {
494
534
495
535
function addIfThenElse ( location , input ) {
496
536
location . isValidated = true
497
- validatorSchemasIds . add ( location . schemaId )
537
+ if ( location . isMerged ) {
538
+ validatorSchemasIds . add ( location . mergedSchemaId )
539
+ } else {
540
+ validatorSchemasIds . add ( location . schemaId )
541
+ }
498
542
499
543
const schema = merge ( { } , location . schema )
500
544
const thenSchema = schema . then
@@ -504,13 +548,13 @@ function addIfThenElse (location, input) {
504
548
delete schema . then
505
549
delete schema . else
506
550
507
- const ifLocation = mergeLocation ( location , 'if' )
508
- const ifSchemaRef = ifLocation . schemaId + ifLocation . jsonPointer
551
+ const ifLocation = location . getPropertyLocation ( 'if' )
552
+ const ifSchemaRef = ifLocation . getSchemaRef ( )
509
553
510
- const thenLocation = mergeLocation ( location , 'then' )
554
+ const thenLocation = location . getPropertyLocation ( 'then' )
511
555
thenLocation . schema = merge ( schema , thenSchema )
512
556
513
- const elseLocation = mergeLocation ( location , 'else' )
557
+ const elseLocation = location . getPropertyLocation ( 'else' )
514
558
elseLocation . schema = merge ( schema , elseSchema )
515
559
516
560
return `
@@ -565,7 +609,7 @@ function buildObject (location) {
565
609
function buildArray ( location ) {
566
610
const schema = location . schema
567
611
568
- let itemsLocation = mergeLocation ( location , 'items' )
612
+ let itemsLocation = location . getPropertyLocation ( 'items' )
569
613
itemsLocation . schema = itemsLocation . schema || { }
570
614
571
615
if ( itemsLocation . schema . $ref ) {
@@ -617,7 +661,7 @@ function buildArray (location) {
617
661
if ( Array . isArray ( itemsSchema ) ) {
618
662
for ( let i = 0 ; i < itemsSchema . length ; i ++ ) {
619
663
const item = itemsSchema [ i ]
620
- const tmpRes = buildValue ( mergeLocation ( itemsLocation , i ) , `obj[${ i } ]` )
664
+ const tmpRes = buildValue ( itemsLocation . getPropertyLocation ( i ) , `obj[${ i } ]` )
621
665
functionCode += `
622
666
if (${ i } < arrayLength) {
623
667
if (${ buildArrayTypeCondition ( item . type , `[${ i } ]` ) } ) {
@@ -713,11 +757,11 @@ function buildMultiTypeSerializer (location, input) {
713
757
714
758
let code = ''
715
759
716
- const locationClone = clone ( location )
717
760
types . forEach ( ( type , index ) => {
761
+ location . schema = { ...location . schema , type }
762
+ const nestedResult = buildSingleTypeSerializer ( location , input )
763
+
718
764
const statement = index === 0 ? 'if' : 'else if'
719
- locationClone . schema . type = type
720
- const nestedResult = buildSingleTypeSerializer ( locationClone , input )
721
765
switch ( type ) {
722
766
case 'null' :
723
767
code += `
@@ -862,10 +906,8 @@ function buildValue (location, input) {
862
906
}
863
907
864
908
if ( schema . allOf ) {
865
- const mergedSchema = clone ( schema )
866
- mergeAllOfSchema ( location , schema , mergedSchema )
867
- schema = mergedSchema
868
- location . schema = mergedSchema
909
+ mergeAllOfSchema ( location , schema , clone ( schema ) )
910
+ schema = location . schema
869
911
}
870
912
871
913
const type = schema . type
@@ -874,14 +916,18 @@ function buildValue (location, input) {
874
916
875
917
if ( type === undefined && ( schema . anyOf || schema . oneOf ) ) {
876
918
location . isValidated = true
877
- validatorSchemasIds . add ( location . schemaId )
919
+ if ( location . isMerged ) {
920
+ validatorSchemasIds . add ( location . mergedSchemaId )
921
+ } else {
922
+ validatorSchemasIds . add ( location . schemaId )
923
+ }
878
924
879
925
const type = schema . anyOf ? 'anyOf' : 'oneOf'
880
- const anyOfLocation = mergeLocation ( location , type )
926
+ const anyOfLocation = location . getPropertyLocation ( type )
881
927
882
928
for ( let index = 0 ; index < location . schema [ type ] . length ; index ++ ) {
883
- const optionLocation = mergeLocation ( anyOfLocation , index )
884
- const schemaRef = optionLocation . schemaId + optionLocation . jsonPointer
929
+ const optionLocation = anyOfLocation . getPropertyLocation ( index )
930
+ const schemaRef = optionLocation . getSchemaRef ( )
885
931
const nestedResult = buildValue ( optionLocation , input )
886
932
code += `
887
933
${ index === 0 ? 'if' : 'else if' } (validator.validate("${ schemaRef } ", ${ input } ))
0 commit comments