@@ -297,11 +297,11 @@ function build (schema, options) {
297
297
switch ( schema . type ) {
298
298
case 'object' :
299
299
main = '$main'
300
- code = buildObject ( location , code , main )
300
+ code = buildObject ( location , code , main , main )
301
301
break
302
302
case 'array' :
303
303
main = '$main'
304
- code = buildArray ( location , code , main )
304
+ code = buildArray ( location , code , main , main )
305
305
schema = location . schema
306
306
break
307
307
case 'string' :
@@ -449,13 +449,13 @@ function addPatternProperties (location) {
449
449
const ifPpKeyExists = `if (/${ regex . replace ( / \\ * \/ / g, '\\/' ) } /.test(keys[i])) {`
450
450
451
451
if ( type === 'object' ) {
452
- code += `${ buildObject ( ppLocation , '' , 'buildObjectPP' + index ) }
452
+ code += `${ buildObject ( ppLocation , '' , 'buildObjectPP' + index , 'buildObjectPP' + index ) }
453
453
${ ifPpKeyExists }
454
454
${ addComma }
455
455
json += serializer.asString(keys[i]) + ':' + buildObjectPP${ index } (obj[keys[i]])
456
456
`
457
457
} else if ( type === 'array' ) {
458
- code += `${ buildArray ( ppLocation , '' , 'buildArrayPP' + index ) }
458
+ code += `${ buildArray ( ppLocation , '' , 'buildArrayPP' + index , 'buildArrayPP' + index ) }
459
459
${ ifPpKeyExists }
460
460
${ addComma }
461
461
json += serializer.asString(keys[i]) + ':' + buildArrayPP${ index } (obj[keys[i]])
@@ -539,12 +539,12 @@ function additionalProperty (location) {
539
539
const format = ap . format
540
540
const stringSerializer = getStringSerializer ( format )
541
541
if ( type === 'object' ) {
542
- code += `${ buildObject ( apLocation , '' , 'buildObjectAP' ) }
542
+ code += `${ buildObject ( apLocation , '' , 'buildObjectAP' , 'buildObjectAP' ) }
543
543
${ addComma }
544
544
json += serializer.asString(keys[i]) + ':' + buildObjectAP(obj[keys[i]])
545
545
`
546
546
} else if ( type === 'array' ) {
547
- code += `${ buildArray ( apLocation , '' , 'buildArrayAP' ) }
547
+ code += `${ buildArray ( apLocation , '' , 'buildArrayAP' , 'buildArrayAP' ) }
548
548
${ addComma }
549
549
json += serializer.asString(keys[i]) + ':' + buildArrayAP(obj[keys[i]])
550
550
`
@@ -734,7 +734,7 @@ function refFinder (ref, location) {
734
734
}
735
735
}
736
736
737
- function buildCode ( location , code , laterCode , name ) {
737
+ function buildCode ( location , code , laterCode , locationPath ) {
738
738
if ( location . schema . $ref ) {
739
739
location = refFinder ( location . schema . $ref , location )
740
740
}
@@ -761,7 +761,7 @@ function buildCode (location, code, laterCode, name) {
761
761
json += ${ asString } + ':'
762
762
`
763
763
764
- const result = nested ( laterCode , name , key , mergeLocation ( propertyLocation , { schema : schema . properties [ key ] } ) , undefined , false )
764
+ const result = nested ( laterCode , locationPath , key , mergeLocation ( propertyLocation , { schema : schema . properties [ key ] } ) , undefined , false )
765
765
code += result . code
766
766
laterCode = result . laterCode
767
767
@@ -790,23 +790,23 @@ function buildCode (location, code, laterCode, name) {
790
790
}
791
791
792
792
if ( schema . allOf ) {
793
- const builtCode = buildCodeWithAllOfs ( location , code , laterCode , name )
793
+ const builtCode = buildCodeWithAllOfs ( location , code , laterCode , locationPath )
794
794
code = builtCode . code
795
795
laterCode = builtCode . laterCode
796
796
}
797
797
798
798
return { code, laterCode }
799
799
}
800
800
801
- function buildCodeWithAllOfs ( location , code , laterCode , name ) {
801
+ function buildCodeWithAllOfs ( location , code , laterCode , locationPath ) {
802
802
if ( location . schema . allOf ) {
803
803
location . schema . allOf . forEach ( ( ss ) => {
804
- const builtCode = buildCodeWithAllOfs ( mergeLocation ( location , { schema : ss } ) , code , laterCode , name )
804
+ const builtCode = buildCodeWithAllOfs ( mergeLocation ( location , { schema : ss } ) , code , laterCode , locationPath )
805
805
code = builtCode . code
806
806
laterCode = builtCode . laterCode
807
807
} )
808
808
} else {
809
- const builtCode = buildCode ( location , code , laterCode , name )
809
+ const builtCode = buildCode ( location , code , laterCode , locationPath )
810
810
811
811
code = builtCode . code
812
812
laterCode = builtCode . laterCode
@@ -815,9 +815,9 @@ function buildCodeWithAllOfs (location, code, laterCode, name) {
815
815
return { code, laterCode }
816
816
}
817
817
818
- function buildInnerObject ( location , name ) {
818
+ function buildInnerObject ( location , locationPath ) {
819
819
const schema = location . schema
820
- const result = buildCodeWithAllOfs ( location , '' , '' , name )
820
+ const result = buildCodeWithAllOfs ( location , '' , '' , locationPath )
821
821
if ( schema . patternProperties ) {
822
822
result . code += addPatternProperties ( location )
823
823
} else if ( schema . additionalProperties && ! schema . patternProperties ) {
@@ -826,7 +826,7 @@ function buildInnerObject (location, name) {
826
826
return result
827
827
}
828
828
829
- function addIfThenElse ( location , name ) {
829
+ function addIfThenElse ( location , locationPath ) {
830
830
let code = ''
831
831
let r
832
832
let laterCode = ''
@@ -851,12 +851,12 @@ function addIfThenElse (location, name) {
851
851
if (valid) {
852
852
`
853
853
if ( merged . if && merged . then ) {
854
- innerR = addIfThenElse ( mergedLocation , name + 'Then' )
854
+ innerR = addIfThenElse ( mergedLocation , locationPath + 'Then' )
855
855
code += innerR . code
856
856
laterCode = innerR . laterCode
857
857
}
858
858
859
- r = buildInnerObject ( mergedLocation , name + 'Then' )
859
+ r = buildInnerObject ( mergedLocation , locationPath + 'Then' )
860
860
code += r . code
861
861
laterCode += r . laterCode
862
862
@@ -871,12 +871,12 @@ function addIfThenElse (location, name) {
871
871
`
872
872
873
873
if ( merged . if && merged . then ) {
874
- innerR = addIfThenElse ( mergedLocation , name + 'Else' )
874
+ innerR = addIfThenElse ( mergedLocation , locationPath + 'Else' )
875
875
code += innerR . code
876
876
laterCode += innerR . laterCode
877
877
}
878
878
879
- r = buildInnerObject ( mergedLocation , name + 'Else' )
879
+ r = buildInnerObject ( mergedLocation , locationPath + 'Else' )
880
880
code += r . code
881
881
laterCode += r . laterCode
882
882
@@ -893,13 +893,14 @@ function toJSON (variableName) {
893
893
`
894
894
}
895
895
896
- function buildObject ( location , code , name ) {
896
+ function buildObject ( location , code , functionName , locationPath ) {
897
897
const schema = location . schema
898
898
if ( schema . $id !== undefined ) {
899
899
schemaReferenceMap . set ( schema . $id , schema )
900
900
}
901
901
code += `
902
- function ${ name } (input) {
902
+ function ${ functionName } (input) {
903
+ // ${ locationPath }
903
904
`
904
905
if ( schema . nullable ) {
905
906
code += `
@@ -909,14 +910,14 @@ function buildObject (location, code, name) {
909
910
`
910
911
}
911
912
912
- if ( objectReferenceSerializersMap . has ( schema ) && objectReferenceSerializersMap . get ( schema ) !== name ) {
913
+ if ( objectReferenceSerializersMap . has ( schema ) && objectReferenceSerializersMap . get ( schema ) !== functionName ) {
913
914
code += `
914
915
return ${ objectReferenceSerializersMap . get ( schema ) } (input)
915
916
}
916
917
`
917
918
return code
918
919
}
919
- objectReferenceSerializersMap . set ( schema , name )
920
+ objectReferenceSerializersMap . set ( schema , functionName )
920
921
921
922
code += `
922
923
var obj = ${ toJSON ( 'input' ) }
@@ -929,9 +930,9 @@ function buildObject (location, code, name) {
929
930
code += `
930
931
var valid
931
932
`
932
- r = addIfThenElse ( location , name )
933
+ r = addIfThenElse ( location , locationPath )
933
934
} else {
934
- r = buildInnerObject ( location , name )
935
+ r = buildInnerObject ( location , locationPath )
935
936
}
936
937
937
938
// Removes the comma if is the last element of the string (in case there are not properties)
@@ -945,13 +946,14 @@ function buildObject (location, code, name) {
945
946
return code
946
947
}
947
948
948
- function buildArray ( location , code , name , key = null ) {
949
+ function buildArray ( location , code , functionName , locationPath , key = null ) {
949
950
let schema = location . schema
950
951
if ( schema . $id !== undefined ) {
951
952
schemaReferenceMap . set ( schema . $id , schema )
952
953
}
953
954
code += `
954
- function ${ name } (obj) {
955
+ function ${ functionName } (obj) {
956
+ // ${ locationPath }
955
957
`
956
958
if ( schema . nullable ) {
957
959
code += `
@@ -984,14 +986,14 @@ function buildArray (location, code, name, key = null) {
984
986
`
985
987
return code
986
988
}
987
- arrayItemsReferenceSerializersMap . set ( schema . items , name )
989
+ arrayItemsReferenceSerializersMap . set ( schema . items , functionName )
988
990
}
989
991
990
992
let result = { code : '' , laterCode : '' }
991
993
const accessor = '[i]'
992
994
if ( Array . isArray ( schema . items ) ) {
993
995
result = schema . items . reduce ( ( res , item , i ) => {
994
- const tmpRes = nested ( laterCode , name , accessor , mergeLocation ( location , { schema : item } ) , i , true )
996
+ const tmpRes = nested ( laterCode , locationPath , accessor , mergeLocation ( location , { schema : item } ) , i , true )
995
997
const condition = `i === ${ i } && ${ buildArrayTypeCondition ( item . type , accessor ) } `
996
998
return {
997
999
code : `${ res . code }
@@ -1004,7 +1006,7 @@ function buildArray (location, code, name, key = null) {
1004
1006
} , result )
1005
1007
1006
1008
if ( schema . additionalItems ) {
1007
- const tmpRes = nested ( laterCode , name , accessor , mergeLocation ( location , { schema : schema . items } ) , undefined , true )
1009
+ const tmpRes = nested ( laterCode , locationPath , accessor , mergeLocation ( location , { schema : schema . items } ) , undefined , true )
1008
1010
result . code += `
1009
1011
else if (i >= ${ schema . items . length } ) {
1010
1012
${ tmpRes . code }
@@ -1018,7 +1020,7 @@ function buildArray (location, code, name, key = null) {
1018
1020
}
1019
1021
`
1020
1022
} else {
1021
- result = nested ( laterCode , name , accessor , mergeLocation ( location , { schema : schema . items } ) , undefined , true )
1023
+ result = nested ( laterCode , locationPath , accessor , mergeLocation ( location , { schema : schema . items } ) , undefined , true )
1022
1024
}
1023
1025
1024
1026
if ( key ) {
@@ -1120,21 +1122,12 @@ function dereferenceOfRefs (location, type) {
1120
1122
return locations
1121
1123
}
1122
1124
1123
- let strNameCounter = 0
1124
- function asFuncName ( str ) {
1125
- // only allow chars that can work
1126
- let rep = str . replace ( / [ ^ a - z A - Z 0 - 9 $ _ ] / g, '' )
1127
-
1128
- if ( rep . length === 0 ) {
1129
- return 'anan' + strNameCounter ++
1130
- } else if ( rep !== str ) {
1131
- rep += strNameCounter ++
1132
- }
1133
-
1134
- return rep
1125
+ let genFuncNameCounter = 0
1126
+ function generateFuncName ( ) {
1127
+ return 'anonymous' + genFuncNameCounter ++
1135
1128
}
1136
1129
1137
- function nested ( laterCode , name , key , location , subKey , isArray ) {
1130
+ function nested ( laterCode , locationPath , key , location , subKey , isArray ) {
1138
1131
subKey = subKey || ''
1139
1132
1140
1133
let schema = location . schema
@@ -1182,21 +1175,21 @@ function nested (laterCode, name, key, location, subKey, isArray) {
1182
1175
code += `json += ${ funcName } (obj${ accessor } )`
1183
1176
break
1184
1177
case 'object' :
1185
- funcName = asFuncName ( name + key + subKey )
1186
- laterCode = buildObject ( location , laterCode , funcName )
1178
+ funcName = generateFuncName ( )
1179
+ laterCode = buildObject ( location , laterCode , funcName , locationPath + key + subKey )
1187
1180
code += `json += ${ funcName } (obj${ accessor } )`
1188
1181
break
1189
1182
case 'array' :
1190
- funcName = asFuncName ( '$arr' + name + key + subKey ) // eslint-disable-line
1191
- laterCode = buildArray ( location , laterCode , funcName , key )
1183
+ funcName = generateFuncName ( )
1184
+ laterCode = buildArray ( location , laterCode , funcName , locationPath + key + subKey , key )
1192
1185
code += `json += ${ funcName } (obj${ accessor } )`
1193
1186
break
1194
1187
case undefined :
1195
1188
if ( schema . anyOf || schema . oneOf ) {
1196
1189
// beware: dereferenceOfRefs has side effects and changes schema.anyOf
1197
1190
const locations = dereferenceOfRefs ( location , schema . anyOf ? 'anyOf' : 'oneOf' )
1198
1191
locations . forEach ( ( location , index ) => {
1199
- const nestedResult = nested ( laterCode , name , key , location , subKey !== '' ? subKey : 'i' + index , isArray )
1192
+ const nestedResult = nested ( laterCode , locationPath , key , location , subKey !== '' ? subKey : 'i' + index , isArray )
1200
1193
// We need a test serializer as the String serializer will not work with
1201
1194
// date/time ajv validations
1202
1195
// see: https://github.com/fastify/fast-json-stringify/issues/325
@@ -1252,7 +1245,7 @@ function nested (laterCode, name, key, location, subKey, isArray) {
1252
1245
sortedTypes . forEach ( ( type , index ) => {
1253
1246
const statement = index === 0 ? 'if' : 'else if'
1254
1247
const tempSchema = Object . assign ( { } , schema , { type } )
1255
- const nestedResult = nested ( laterCode , name , key , mergeLocation ( location , { schema : tempSchema } ) , subKey , isArray )
1248
+ const nestedResult = nested ( laterCode , locationPath , key , mergeLocation ( location , { schema : tempSchema } ) , subKey , isArray )
1256
1249
switch ( type ) {
1257
1250
case 'string' : {
1258
1251
code += `
0 commit comments