@@ -408,176 +408,86 @@ function addPatternProperties (location) {
408408 for (var i = 0; i < keys.length; i++) {
409409 if (properties[keys[i]]) continue
410410 `
411+ let laterCode = ''
412+
411413 Object . keys ( pp ) . forEach ( ( regex , index ) => {
412414 let ppLocation = mergeLocation ( location , { schema : pp [ regex ] } )
413415 if ( pp [ regex ] . $ref ) {
414416 ppLocation = refFinder ( pp [ regex ] . $ref , location )
415417 pp [ regex ] = ppLocation . schema
416418 }
417- const type = pp [ regex ] . type
418- const format = pp [ regex ] . format
419- const stringSerializer = getStringSerializer ( format )
419+
420420 try {
421421 RegExp ( regex )
422422 } catch ( err ) {
423423 throw new Error ( `${ err . message } . Found at ${ regex } matching ${ JSON . stringify ( pp [ regex ] ) } ` )
424424 }
425425
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
483428 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+ }
486435 `
487436 } )
488437 if ( schema . additionalProperties ) {
489- code += additionalProperty ( location )
438+ const additionalPropertyCode = additionalProperty ( location )
439+ code += additionalPropertyCode . code
440+ laterCode += additionalPropertyCode . laterCode
490441 }
491442
492443 code += `
493444 }
494445 `
495- return code
446+ return { code, laterCode }
496447}
497448
498449function additionalProperty ( location ) {
499450 let ap = location . schema . additionalProperties
500451 let code = ''
501452 if ( ap === true ) {
502- return `
453+ code += `
503454 if (obj[keys[i]] !== undefined && typeof obj[keys[i]] !== 'function' && typeof obj[keys[i]] !== 'symbol') {
504455 ${ addComma }
505456 json += serializer.asString(keys[i]) + ':' + JSON.stringify(obj[keys[i]])
506457 }
507458 `
459+
460+ return { code, laterCode : '' }
508461 }
509462 let apLocation = mergeLocation ( location , { schema : ap } )
510463 if ( ap . $ref ) {
511464 apLocation = refFinder ( ap . $ref , location )
512465 ap = apLocation . schema
513466 }
514467
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 }
570477}
571478
572479function addAdditionalProperties ( location ) {
573- return `
480+ const additionalPropertyCode = additionalProperty ( location )
481+ const code = `
574482 var properties = ${ JSON . stringify ( location . schema . properties ) } || {}
575483 var keys = Object.keys(obj)
576484 for (var i = 0; i < keys.length; i++) {
577485 if (properties[keys[i]]) continue
578- ${ additionalProperty ( location ) }
486+ ${ additionalPropertyCode . code }
579487 }
580488 `
489+
490+ return { code, laterCode : additionalPropertyCode . laterCode }
581491}
582492
583493function idFinder ( schema , searchedId ) {
@@ -796,9 +706,13 @@ function buildInnerObject (location, locationPath) {
796706 const schema = location . schema
797707 const result = buildCodeWithAllOfs ( location , '' , '' , locationPath )
798708 if ( schema . patternProperties ) {
799- result . code += addPatternProperties ( location )
709+ const { code, laterCode } = addPatternProperties ( location )
710+ result . code += code
711+ result . laterCode += laterCode
800712 } 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
802716 }
803717 return result
804718}
@@ -923,7 +837,7 @@ function buildObject (location, code, functionName, locationPath) {
923837 return code
924838}
925839
926- function buildArray ( location , code , functionName , locationPath , isObjectProperty = false ) {
840+ function buildArray ( location , code , functionName , locationPath ) {
927841 let schema = location . schema
928842 if ( schema . $id !== undefined ) {
929843 schemaReferenceMap . set ( schema . $id , schema )
@@ -1000,13 +914,11 @@ function buildArray (location, code, functionName, locationPath, isObjectPropert
1000914 result = buildValue ( laterCode , locationPath + accessor , 'obj[i]' , mergeLocation ( location , { schema : schema . items } ) )
1001915 }
1002916
1003- if ( isObjectProperty ) {
1004- code += `
1005- if(!Array.isArray(obj)) {
917+ code += `
918+ if (!Array.isArray(obj)) {
1006919 throw new TypeError(\`The value '$\{obj}' does not match schema definition.\`)
1007920 }
1008- `
1009- }
921+ `
1010922
1011923 code += 'const arrayLength = obj.length\n'
1012924 if ( largeArrayMechanism !== 'default' ) {
@@ -1154,7 +1066,7 @@ function buildValue (laterCode, locationPath, input, location) {
11541066 break
11551067 case 'array' :
11561068 funcName = generateFuncName ( )
1157- laterCode = buildArray ( location , laterCode , funcName , locationPath , true )
1069+ laterCode = buildArray ( location , laterCode , funcName , locationPath )
11581070 code += `json += ${ funcName } (${ input } )`
11591071 break
11601072 case undefined :
0 commit comments