@@ -357,7 +357,7 @@ function buildInnerObject (context, location) {
357357 )
358358 const hasRequiredProperties = requiredProperties . includes ( propertiesKeys [ 0 ] )
359359
360- let code = 'let value\n '
360+ let code = ''
361361
362362 for ( const key of requiredProperties ) {
363363 if ( ! propertiesKeys . includes ( key ) ) {
@@ -381,15 +381,16 @@ function buildInnerObject (context, location) {
381381 }
382382
383383 const sanitizedKey = JSON . stringify ( key )
384+ const value = 'value_' + key . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '_' )
384385 const defaultValue = propertyLocation . schema . default
385386 const isRequired = requiredProperties . includes ( key )
386387
387388 code += `
388- value = obj[${ sanitizedKey } ]
389- if (value !== undefined) {
389+ let ${ value } = obj[${ sanitizedKey } ]
390+ if (${ value } !== undefined) {
390391 ${ addComma }
391392 json += ${ JSON . stringify ( sanitizedKey + ':' ) }
392- ${ buildValue ( context , propertyLocation , ' value' ) }
393+ ${ buildValue ( context , propertyLocation , ` ${ value } ` ) }
393394 }`
394395
395396 if ( defaultValue !== undefined ) {
@@ -570,18 +571,18 @@ function buildArray (context, location) {
570571
571572 functionCode += `
572573 const arrayEnd = arrayLength - 1
573- let value
574574 let json = ''
575575 `
576576
577577 if ( Array . isArray ( itemsSchema ) ) {
578578 for ( let i = 0 ; i < itemsSchema . length ; i ++ ) {
579579 const item = itemsSchema [ i ]
580- functionCode += `value = obj[${ i } ]`
581- const tmpRes = buildValue ( context , itemsLocation . getPropertyLocation ( i ) , 'value' )
580+ const value = `value_${ i } `
581+ functionCode += `const ${ value } = obj[${ i } ]`
582+ const tmpRes = buildValue ( context , itemsLocation . getPropertyLocation ( i ) , value )
582583 functionCode += `
583584 if (${ i } < arrayLength) {
584- if (${ buildArrayTypeCondition ( item . type , ' value' ) } ) {
585+ if (${ buildArrayTypeCondition ( item . type , value ) } ) {
585586 ${ tmpRes }
586587 if (${ i } < arrayEnd) {
587588 json += JSON_STR_COMMA
@@ -596,8 +597,7 @@ function buildArray (context, location) {
596597 if ( schema . additionalItems ) {
597598 functionCode += `
598599 for (let i = ${ itemsSchema . length } ; i < arrayLength; i++) {
599- value = obj[i]
600- json += JSON.stringify(value)
600+ json += JSON.stringify(obj[i])
601601 if (i < arrayEnd) {
602602 json += JSON_STR_COMMA
603603 }
@@ -607,7 +607,7 @@ function buildArray (context, location) {
607607 const code = buildValue ( context , itemsLocation , 'value' )
608608 functionCode += `
609609 for (let i = 0; i < arrayLength; i++) {
610- value = obj[i]
610+ const value = obj[i]
611611 ${ code }
612612 if (i < arrayEnd) {
613613 json += JSON_STR_COMMA
@@ -627,33 +627,33 @@ function buildArrayTypeCondition (type, accessor) {
627627 let condition
628628 switch ( type ) {
629629 case 'null' :
630- condition = 'value === null'
630+ condition = ` ${ accessor } === null`
631631 break
632632 case 'string' :
633- condition = `typeof value === 'string' ||
634- value === null ||
635- value instanceof Date ||
636- value instanceof RegExp ||
633+ condition = `typeof ${ accessor } === 'string' ||
634+ ${ accessor } === null ||
635+ ${ accessor } instanceof Date ||
636+ ${ accessor } instanceof RegExp ||
637637 (
638- typeof value === "object" &&
639- typeof value .toString === "function" &&
640- value .toString !== Object.prototype.toString
638+ typeof ${ accessor } === "object" &&
639+ typeof ${ accessor } .toString === "function" &&
640+ ${ accessor } .toString !== Object.prototype.toString
641641 )`
642642 break
643643 case 'integer' :
644- condition = ' Number.isInteger(value)'
644+ condition = ` Number.isInteger(${ accessor } )`
645645 break
646646 case 'number' :
647- condition = ' Number.isFinite(value)'
647+ condition = ` Number.isFinite(${ accessor } )`
648648 break
649649 case 'boolean' :
650- condition = ' typeof value === \ 'boolean\''
650+ condition = ` typeof ${ accessor } === 'boolean'`
651651 break
652652 case 'object' :
653- condition = 'value && typeof value === \ 'object\ ' && value .constructor === Object'
653+ condition = ` ${ accessor } && typeof ${ accessor } === 'object' && ${ accessor } .constructor === Object`
654654 break
655655 case 'array' :
656- condition = ' Array.isArray(value)'
656+ condition = ` Array.isArray(${ accessor } )`
657657 break
658658 default :
659659 if ( Array . isArray ( type ) ) {
0 commit comments