@@ -408,176 +408,86 @@ function addPatternProperties (location) {
408
408
for (var i = 0; i < keys.length; i++) {
409
409
if (properties[keys[i]]) continue
410
410
`
411
+ let laterCode = ''
412
+
411
413
Object . keys ( pp ) . forEach ( ( regex , index ) => {
412
414
let ppLocation = mergeLocation ( location , { schema : pp [ regex ] } )
413
415
if ( pp [ regex ] . $ref ) {
414
416
ppLocation = refFinder ( pp [ regex ] . $ref , location )
415
417
pp [ regex ] = ppLocation . schema
416
418
}
417
- const type = pp [ regex ] . type
418
- const format = pp [ regex ] . format
419
- const stringSerializer = getStringSerializer ( format )
419
+
420
420
try {
421
421
RegExp ( regex )
422
422
} catch ( err ) {
423
423
throw new Error ( `${ err . message } . Found at ${ regex } matching ${ JSON . stringify ( pp [ regex ] ) } ` )
424
424
}
425
425
426
- const ifPpKeyExists = `if (/${ regex . replace ( / \\ * \/ / g, '\\/' ) } /.test(keys[i])) {`
427
-
428
- if ( type === 'object' ) {
429
- code += `${ buildObject ( ppLocation , '' , 'buildObjectPP' + index , 'buildObjectPP' + index ) }
430
- ${ ifPpKeyExists }
431
- ${ addComma }
432
- json += serializer.asString(keys[i]) + ':' + buildObjectPP${ index } (obj[keys[i]])
433
- `
434
- } else if ( type === 'array' ) {
435
- code += `${ buildArray ( ppLocation , '' , 'buildArrayPP' + index , 'buildArrayPP' + index ) }
436
- ${ ifPpKeyExists }
437
- ${ addComma }
438
- json += serializer.asString(keys[i]) + ':' + buildArrayPP${ index } (obj[keys[i]])
439
- `
440
- } else if ( type === 'null' ) {
441
- code += `
442
- ${ ifPpKeyExists }
443
- ${ addComma }
444
- json += serializer.asString(keys[i]) +':null'
445
- `
446
- } else if ( type === 'string' ) {
447
- code += `
448
- ${ ifPpKeyExists }
449
- ${ addComma }
450
- json += serializer.asString(keys[i]) + ':' + ${ stringSerializer } (obj[keys[i]])
451
- `
452
- } else if ( type === 'integer' ) {
453
- code += `
454
- ${ ifPpKeyExists }
455
- ${ addComma }
456
- json += serializer.asString(keys[i]) + ':' + serializer.asInteger(obj[keys[i]])
457
- `
458
- } else if ( type === 'number' ) {
459
- code += `
460
- ${ ifPpKeyExists }
461
- ${ addComma }
462
- json += serializer.asString(keys[i]) + ':' + serializer.asNumber(obj[keys[i]])
463
- `
464
- } else if ( type === 'boolean' ) {
465
- code += `
466
- ${ ifPpKeyExists }
467
- ${ addComma }
468
- json += serializer.asString(keys[i]) + ':' + serializer.asBoolean(obj[keys[i]])
469
- `
470
- } else if ( type === undefined ) {
471
- code += `
472
- ${ ifPpKeyExists }
473
- ${ addComma }
474
- json += serializer.asString(keys[i]) + ':' + serializer.asAny(obj[keys[i]])
475
- `
476
- } else {
477
- code += `
478
- ${ ifPpKeyExists }
479
- throw new Error('Cannot coerce ' + obj[keys[i]] + ' to ' + ${ JSON . stringify ( type ) } )
480
- `
481
- }
482
-
426
+ const valueCode = buildValue ( '' , '' , 'obj[keys[i]]' , ppLocation )
427
+ laterCode += valueCode . laterCode
483
428
code += `
484
- continue
485
- }
429
+ if (/${ regex . replace ( / \\ * \/ / g, '\\/' ) } /.test(keys[i])) {
430
+ ${ addComma }
431
+ json += serializer.asString(keys[i]) + ':'
432
+ ${ valueCode . code }
433
+ continue
434
+ }
486
435
`
487
436
} )
488
437
if ( schema . additionalProperties ) {
489
- code += additionalProperty ( location )
438
+ const additionalPropertyCode = additionalProperty ( location )
439
+ code += additionalPropertyCode . code
440
+ laterCode += additionalPropertyCode . laterCode
490
441
}
491
442
492
443
code += `
493
444
}
494
445
`
495
- return code
446
+ return { code, laterCode }
496
447
}
497
448
498
449
function additionalProperty ( location ) {
499
450
let ap = location . schema . additionalProperties
500
451
let code = ''
501
452
if ( ap === true ) {
502
- return `
453
+ code += `
503
454
if (obj[keys[i]] !== undefined && typeof obj[keys[i]] !== 'function' && typeof obj[keys[i]] !== 'symbol') {
504
455
${ addComma }
505
456
json += serializer.asString(keys[i]) + ':' + JSON.stringify(obj[keys[i]])
506
457
}
507
458
`
459
+
460
+ return { code, laterCode : '' }
508
461
}
509
462
let apLocation = mergeLocation ( location , { schema : ap } )
510
463
if ( ap . $ref ) {
511
464
apLocation = refFinder ( ap . $ref , location )
512
465
ap = apLocation . schema
513
466
}
514
467
515
- const type = ap . type
516
- const format = ap . format
517
- const stringSerializer = getStringSerializer ( format )
518
- if ( type === 'object' ) {
519
- code += `${ buildObject ( apLocation , '' , 'buildObjectAP' , 'buildObjectAP' ) }
520
- ${ addComma }
521
- json += serializer.asString(keys[i]) + ':' + buildObjectAP(obj[keys[i]])
522
- `
523
- } else if ( type === 'array' ) {
524
- code += `${ buildArray ( apLocation , '' , 'buildArrayAP' , 'buildArrayAP' ) }
525
- ${ addComma }
526
- json += serializer.asString(keys[i]) + ':' + buildArrayAP(obj[keys[i]])
527
- `
528
- } else if ( type === 'null' ) {
529
- code += `
530
- ${ addComma }
531
- json += serializer.asString(keys[i]) +':null'
532
- `
533
- } else if ( type === 'string' ) {
534
- code += `
535
- ${ addComma }
536
- json += serializer.asString(keys[i]) + ':' + ${ stringSerializer } (obj[keys[i]])
537
- `
538
- } else if ( type === 'integer' ) {
539
- code += `
540
- var t = Number(obj[keys[i]])
541
- if (!isNaN(t)) {
542
- ${ addComma }
543
- json += serializer.asString(keys[i]) + ':' + t
544
- }
545
- `
546
- } else if ( type === 'number' ) {
547
- code += `
548
- var t = Number(obj[keys[i]])
549
- if (!isNaN(t)) {
550
- ${ addComma }
551
- json += serializer.asString(keys[i]) + ':' + t
552
- }
553
- `
554
- } else if ( type === 'boolean' ) {
555
- code += `
556
- ${ addComma }
557
- json += serializer.asString(keys[i]) + ':' + serializer.asBoolean(obj[keys[i]])
558
- `
559
- } else if ( type === undefined ) {
560
- code += `
561
- ${ addComma }
562
- json += serializer.asString(keys[i]) + ':' + serializer.asAny(obj[keys[i]])
563
- `
564
- } else {
565
- code += `
566
- throw new Error('Cannot coerce ' + obj[keys[i]] + ' to ' + ${ JSON . stringify ( type ) } )
567
- `
568
- }
569
- return code
468
+ const valueCode = buildValue ( '' , '' , 'obj[keys[i]]' , apLocation )
469
+
470
+ code += `
471
+ ${ addComma }
472
+ json += serializer.asString(keys[i]) + ':'
473
+ ${ valueCode . code }
474
+ `
475
+
476
+ return { code, laterCode : valueCode . laterCode }
570
477
}
571
478
572
479
function addAdditionalProperties ( location ) {
573
- return `
480
+ const additionalPropertyCode = additionalProperty ( location )
481
+ const code = `
574
482
var properties = ${ JSON . stringify ( location . schema . properties ) } || {}
575
483
var keys = Object.keys(obj)
576
484
for (var i = 0; i < keys.length; i++) {
577
485
if (properties[keys[i]]) continue
578
- ${ additionalProperty ( location ) }
486
+ ${ additionalPropertyCode . code }
579
487
}
580
488
`
489
+
490
+ return { code, laterCode : additionalPropertyCode . laterCode }
581
491
}
582
492
583
493
function idFinder ( schema , searchedId ) {
@@ -796,9 +706,13 @@ function buildInnerObject (location, locationPath) {
796
706
const schema = location . schema
797
707
const result = buildCodeWithAllOfs ( location , '' , '' , locationPath )
798
708
if ( schema . patternProperties ) {
799
- result . code += addPatternProperties ( location )
709
+ const { code, laterCode } = addPatternProperties ( location )
710
+ result . code += code
711
+ result . laterCode += laterCode
800
712
} else if ( schema . additionalProperties && ! schema . patternProperties ) {
801
- result . code += addAdditionalProperties ( location )
713
+ const { code, laterCode } = addAdditionalProperties ( location )
714
+ result . code += code
715
+ result . laterCode += laterCode
802
716
}
803
717
return result
804
718
}
@@ -923,7 +837,7 @@ function buildObject (location, code, functionName, locationPath) {
923
837
return code
924
838
}
925
839
926
- function buildArray ( location , code , functionName , locationPath , isObjectProperty = false ) {
840
+ function buildArray ( location , code , functionName , locationPath ) {
927
841
let schema = location . schema
928
842
if ( schema . $id !== undefined ) {
929
843
schemaReferenceMap . set ( schema . $id , schema )
@@ -1000,13 +914,11 @@ function buildArray (location, code, functionName, locationPath, isObjectPropert
1000
914
result = buildValue ( laterCode , locationPath + accessor , 'obj[i]' , mergeLocation ( location , { schema : schema . items } ) )
1001
915
}
1002
916
1003
- if ( isObjectProperty ) {
1004
- code += `
1005
- if(!Array.isArray(obj)) {
917
+ code += `
918
+ if (!Array.isArray(obj)) {
1006
919
throw new TypeError(\`The value '$\{obj}' does not match schema definition.\`)
1007
920
}
1008
- `
1009
- }
921
+ `
1010
922
1011
923
code += 'const arrayLength = obj.length\n'
1012
924
if ( largeArrayMechanism !== 'default' ) {
@@ -1154,7 +1066,7 @@ function buildValue (laterCode, locationPath, input, location) {
1154
1066
break
1155
1067
case 'array' :
1156
1068
funcName = generateFuncName ( )
1157
- laterCode = buildArray ( location , laterCode , funcName , locationPath , true )
1069
+ laterCode = buildArray ( location , laterCode , funcName , locationPath )
1158
1070
code += `json += ${ funcName } (${ input } )`
1159
1071
break
1160
1072
case undefined :
0 commit comments