@@ -10,6 +10,7 @@ const validate = require('./lib/schema-validator')
10
10
const Serializer = require ( './lib/serializer' )
11
11
const Validator = require ( './lib/validator' )
12
12
const RefResolver = require ( './lib/ref-resolver' )
13
+ const Location = require ( './lib/location' )
13
14
14
15
let largeArraySize = 2e4
15
16
let largeArrayMechanism = 'default'
@@ -40,22 +41,13 @@ function isValidSchema (schema, name) {
40
41
}
41
42
}
42
43
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
49
- }
50
- }
51
-
52
44
function resolveRef ( location , ref ) {
53
45
let hashIndex = ref . indexOf ( '#' )
54
46
if ( hashIndex === - 1 ) {
55
47
hashIndex = ref . length
56
48
}
57
49
58
- const schemaId = ref . slice ( 0 , hashIndex ) || location . schemaId
50
+ const schemaId = ref . slice ( 0 , hashIndex ) || location . getOriginSchemaId ( )
59
51
const jsonPointer = ref . slice ( hashIndex ) || '#'
60
52
61
53
const schema = refResolver . getSchema ( schemaId , jsonPointer )
@@ -68,11 +60,12 @@ function resolveRef (location, ref) {
68
60
validatorSchemasIds . add ( schemaId )
69
61
}
70
62
63
+ const newLocation = new Location ( schema , schemaId , jsonPointer , location . isValidated )
71
64
if ( schema . $ref !== undefined ) {
72
- return resolveRef ( { schema , schemaId , jsonPointer } , schema . $ref )
65
+ return resolveRef ( newLocation , schema . $ref )
73
66
}
74
67
75
- return { schema , schemaId , jsonPointer , isValidated : location . isValidated }
68
+ return newLocation
76
69
}
77
70
78
71
const contextFunctionsNamesBySchema = new Map ( )
@@ -125,7 +118,7 @@ function build (schema, options) {
125
118
}
126
119
}
127
120
128
- const location = { schema, schemaId : rootSchemaId , jsonPointer : '#' , isValidated : false }
121
+ const location = new Location ( schema , rootSchemaId )
129
122
const code = buildValue ( location , 'input' )
130
123
131
124
const contextFunctionCode = `
@@ -253,17 +246,17 @@ function buildExtraObjectPropertiesSerializer (location) {
253
246
) continue
254
247
`
255
248
256
- const patternPropertiesLocation = mergeLocation ( location , 'patternProperties' )
249
+ const patternPropertiesLocation = location . getPropertyLocation ( 'patternProperties' )
257
250
const patternPropertiesSchema = patternPropertiesLocation . schema
258
251
259
252
if ( patternPropertiesSchema !== undefined ) {
260
253
for ( const propertyKey in patternPropertiesSchema ) {
261
- const propertyLocation = mergeLocation ( patternPropertiesLocation , propertyKey )
254
+ const propertyLocation = patternPropertiesLocation . getPropertyLocation ( propertyKey )
262
255
263
256
try {
264
257
RegExp ( propertyKey )
265
258
} catch ( err ) {
266
- const jsonPointer = propertyLocation . schema + propertyLocation . jsonPointer
259
+ const jsonPointer = propertyLocation . getSchemaRef ( )
267
260
throw new Error ( `${ err . message } . Invalid pattern property regexp key ${ propertyKey } at ${ jsonPointer } ` )
268
261
}
269
262
@@ -278,7 +271,7 @@ function buildExtraObjectPropertiesSerializer (location) {
278
271
}
279
272
}
280
273
281
- const additionalPropertiesLocation = mergeLocation ( location , 'additionalProperties' )
274
+ const additionalPropertiesLocation = location . getPropertyLocation ( 'additionalProperties' )
282
275
const additionalPropertiesSchema = additionalPropertiesLocation . schema
283
276
284
277
if ( additionalPropertiesSchema !== undefined ) {
@@ -288,7 +281,7 @@ function buildExtraObjectPropertiesSerializer (location) {
288
281
json += serializer.asString(key) + ':' + JSON.stringify(value)
289
282
`
290
283
} else {
291
- const propertyLocation = mergeLocation ( location , 'additionalProperties' )
284
+ const propertyLocation = location . getPropertyLocation ( 'additionalProperties' )
292
285
code += `
293
286
${ addComma }
294
287
json += serializer.asString(key) + ':'
@@ -309,9 +302,9 @@ function buildInnerObject (location) {
309
302
310
303
let code = ''
311
304
312
- const propertiesLocation = mergeLocation ( location , 'properties' )
305
+ const propertiesLocation = location . getPropertyLocation ( 'properties' )
313
306
Object . keys ( schema . properties || { } ) . forEach ( ( key ) => {
314
- let propertyLocation = mergeLocation ( propertiesLocation , key )
307
+ let propertyLocation = propertiesLocation . getPropertyLocation ( key )
315
308
if ( propertyLocation . schema . $ref ) {
316
309
propertyLocation = resolveRef ( location , propertyLocation . schema . $ref )
317
310
}
@@ -362,13 +355,13 @@ function buildInnerObject (location) {
362
355
}
363
356
364
357
function mergeAllOfSchema ( location , schema , mergedSchema ) {
365
- const allOfLocation = mergeLocation ( location , 'allOf' )
358
+ const allOfLocation = location . getPropertyLocation ( 'allOf' )
366
359
367
360
for ( let i = 0 ; i < schema . allOf . length ; i ++ ) {
368
361
let allOfSchema = schema . allOf [ i ]
369
362
370
363
if ( allOfSchema . $ref ) {
371
- const allOfSchemaLocation = mergeLocation ( allOfLocation , i )
364
+ const allOfSchemaLocation = allOfLocation . getPropertyLocation ( i )
372
365
allOfSchema = resolveRef ( allOfSchemaLocation , allOfSchema . $ref ) . schema
373
366
}
374
367
@@ -457,13 +450,12 @@ function mergeAllOfSchema (location, schema, mergedSchema) {
457
450
458
451
mergedSchema . $id = `merged_${ randomUUID ( ) } `
459
452
refResolver . addSchema ( mergedSchema )
460
- location . schemaId = mergedSchema . $id
461
- location . jsonPointer = '#'
453
+ location . addMergedSchema ( mergedSchema , mergedSchema . $id )
462
454
}
463
455
464
456
function addIfThenElse ( location , input ) {
465
457
location . isValidated = true
466
- validatorSchemasIds . add ( location . schemaId )
458
+ validatorSchemasIds . add ( location . getSchemaId ( ) )
467
459
468
460
const schema = merge ( { } , location . schema )
469
461
const thenSchema = schema . then
@@ -473,13 +465,13 @@ function addIfThenElse (location, input) {
473
465
delete schema . then
474
466
delete schema . else
475
467
476
- const ifLocation = mergeLocation ( location , 'if' )
477
- const ifSchemaRef = ifLocation . schemaId + ifLocation . jsonPointer
468
+ const ifLocation = location . getPropertyLocation ( 'if' )
469
+ const ifSchemaRef = ifLocation . getSchemaRef ( )
478
470
479
- const thenLocation = mergeLocation ( location , 'then' )
471
+ const thenLocation = location . getPropertyLocation ( 'then' )
480
472
thenLocation . schema = merge ( schema , thenSchema )
481
473
482
- const elseLocation = mergeLocation ( location , 'else' )
474
+ const elseLocation = location . getPropertyLocation ( 'else' )
483
475
elseLocation . schema = merge ( schema , elseSchema )
484
476
485
477
return `
@@ -508,10 +500,14 @@ function buildObject (location) {
508
500
const functionName = generateFuncName ( )
509
501
contextFunctionsNamesBySchema . set ( schema , functionName )
510
502
511
- const schemaId = location . schemaId === rootSchemaId ? '' : location . schemaId
503
+ let schemaRef = location . getSchemaRef ( )
504
+ if ( schemaRef . startsWith ( rootSchemaId ) ) {
505
+ schemaRef = schemaRef . replace ( rootSchemaId , '' )
506
+ }
507
+
512
508
let functionCode = `
513
509
function ${ functionName } (input) {
514
- // ${ schemaId + location . jsonPointer }
510
+ // ${ schemaRef }
515
511
`
516
512
517
513
functionCode += `
@@ -534,7 +530,7 @@ function buildObject (location) {
534
530
function buildArray ( location ) {
535
531
const schema = location . schema
536
532
537
- let itemsLocation = mergeLocation ( location , 'items' )
533
+ let itemsLocation = location . getPropertyLocation ( 'items' )
538
534
itemsLocation . schema = itemsLocation . schema || { }
539
535
540
536
if ( itemsLocation . schema . $ref ) {
@@ -550,10 +546,14 @@ function buildArray (location) {
550
546
const functionName = generateFuncName ( )
551
547
contextFunctionsNamesBySchema . set ( schema , functionName )
552
548
553
- const schemaId = location . schemaId === rootSchemaId ? '' : location . schemaId
549
+ let schemaRef = location . getSchemaRef ( )
550
+ if ( schemaRef . startsWith ( rootSchemaId ) ) {
551
+ schemaRef = schemaRef . replace ( rootSchemaId , '' )
552
+ }
553
+
554
554
let functionCode = `
555
555
function ${ functionName } (obj) {
556
- // ${ schemaId + location . jsonPointer }
556
+ // ${ schemaRef }
557
557
`
558
558
559
559
functionCode += `
@@ -586,7 +586,7 @@ function buildArray (location) {
586
586
if ( Array . isArray ( itemsSchema ) ) {
587
587
for ( let i = 0 ; i < itemsSchema . length ; i ++ ) {
588
588
const item = itemsSchema [ i ]
589
- const tmpRes = buildValue ( mergeLocation ( itemsLocation , i ) , `obj[${ i } ]` )
589
+ const tmpRes = buildValue ( itemsLocation . getPropertyLocation ( i ) , `obj[${ i } ]` )
590
590
functionCode += `
591
591
if (${ i } < arrayLength) {
592
592
if (${ buildArrayTypeCondition ( item . type , `[${ i } ]` ) } ) {
@@ -682,11 +682,11 @@ function buildMultiTypeSerializer (location, input) {
682
682
683
683
let code = ''
684
684
685
- const locationClone = clone ( location )
686
685
types . forEach ( ( type , index ) => {
686
+ location . schema = { ...location . schema , type }
687
+ const nestedResult = buildSingleTypeSerializer ( location , input )
688
+
687
689
const statement = index === 0 ? 'if' : 'else if'
688
- locationClone . schema . type = type
689
- const nestedResult = buildSingleTypeSerializer ( locationClone , input )
690
690
switch ( type ) {
691
691
case 'null' :
692
692
code += `
@@ -831,10 +831,8 @@ function buildValue (location, input) {
831
831
}
832
832
833
833
if ( schema . allOf ) {
834
- const mergedSchema = clone ( schema )
835
- mergeAllOfSchema ( location , schema , mergedSchema )
836
- schema = mergedSchema
837
- location . schema = mergedSchema
834
+ mergeAllOfSchema ( location , schema , clone ( schema ) )
835
+ schema = location . schema
838
836
}
839
837
840
838
const type = schema . type
@@ -843,14 +841,14 @@ function buildValue (location, input) {
843
841
844
842
if ( type === undefined && ( schema . anyOf || schema . oneOf ) ) {
845
843
location . isValidated = true
846
- validatorSchemasIds . add ( location . schemaId )
844
+ validatorSchemasIds . add ( location . getSchemaId ( ) )
847
845
848
846
const type = schema . anyOf ? 'anyOf' : 'oneOf'
849
- const anyOfLocation = mergeLocation ( location , type )
847
+ const anyOfLocation = location . getPropertyLocation ( type )
850
848
851
849
for ( let index = 0 ; index < location . schema [ type ] . length ; index ++ ) {
852
- const optionLocation = mergeLocation ( anyOfLocation , index )
853
- const schemaRef = optionLocation . schemaId + optionLocation . jsonPointer
850
+ const optionLocation = anyOfLocation . getPropertyLocation ( index )
851
+ const schemaRef = optionLocation . getSchemaRef ( )
854
852
const nestedResult = buildValue ( optionLocation , input )
855
853
code += `
856
854
${ index === 0 ? 'if' : 'else if' } (validator.validate("${ schemaRef } ", ${ input } ))
0 commit comments