@@ -333,7 +333,7 @@ function buildInnerObject (context, location) {
333
333
)
334
334
const hasRequiredProperties = requiredProperties . includes ( propertiesKeys [ 0 ] )
335
335
336
- let code = ''
336
+ let code = 'let value\n '
337
337
338
338
for ( const key of requiredProperties ) {
339
339
if ( ! propertiesKeys . includes ( key ) ) {
@@ -360,10 +360,11 @@ function buildInnerObject (context, location) {
360
360
const isRequired = requiredProperties . includes ( key )
361
361
362
362
code += `
363
- if (obj[${ sanitizedKey } ] !== undefined) {
363
+ value = obj[${ sanitizedKey } ]
364
+ if (value !== undefined) {
364
365
${ addComma }
365
366
json += ${ JSON . stringify ( sanitizedKey + ':' ) }
366
- ${ buildValue ( context , propertyLocation , `obj[ ${ sanitizedKey } ]` ) }
367
+ ${ buildValue ( context , propertyLocation , 'value' ) }
367
368
}`
368
369
369
370
if ( defaultValue !== undefined ) {
@@ -543,13 +544,15 @@ function buildArray (context, location) {
543
544
}
544
545
545
546
functionCode += `
547
+ let value
546
548
let jsonOutput = ''
547
549
`
548
550
549
551
if ( Array . isArray ( itemsSchema ) ) {
550
552
for ( let i = 0 ; i < itemsSchema . length ; i ++ ) {
551
553
const item = itemsSchema [ i ]
552
- const tmpRes = buildValue ( context , itemsLocation . getPropertyLocation ( i ) , `obj[${ i } ]` )
554
+ functionCode += `value = obj[${ i } ]`
555
+ const tmpRes = buildValue ( context , itemsLocation . getPropertyLocation ( i ) , 'value' )
553
556
functionCode += `
554
557
if (${ i } < arrayLength) {
555
558
if (${ buildArrayTypeCondition ( item . type , `[${ i } ]` ) } ) {
@@ -600,33 +603,33 @@ function buildArrayTypeCondition (type, accessor) {
600
603
let condition
601
604
switch ( type ) {
602
605
case 'null' :
603
- condition = `obj ${ accessor } === null`
606
+ condition = 'value === null'
604
607
break
605
608
case 'string' :
606
- condition = `typeof obj ${ accessor } === 'string' ||
607
- obj ${ accessor } === null ||
608
- obj ${ accessor } instanceof Date ||
609
- obj ${ accessor } instanceof RegExp ||
609
+ condition = `typeof value === 'string' ||
610
+ value === null ||
611
+ value instanceof Date ||
612
+ value instanceof RegExp ||
610
613
(
611
- typeof obj ${ accessor } === "object" &&
612
- typeof obj ${ accessor } .toString === "function" &&
613
- obj ${ accessor } .toString !== Object.prototype.toString
614
+ typeof value === "object" &&
615
+ typeof value .toString === "function" &&
616
+ value .toString !== Object.prototype.toString
614
617
)`
615
618
break
616
619
case 'integer' :
617
- condition = ` Number.isInteger(obj ${ accessor } )`
620
+ condition = ' Number.isInteger(value)'
618
621
break
619
622
case 'number' :
620
- condition = ` Number.isFinite(obj ${ accessor } )`
623
+ condition = ' Number.isFinite(value)'
621
624
break
622
625
case 'boolean' :
623
- condition = ` typeof obj ${ accessor } === 'boolean'`
626
+ condition = ' typeof value === \ 'boolean\''
624
627
break
625
628
case 'object' :
626
- condition = `obj ${ accessor } && typeof obj ${ accessor } === 'object' && obj ${ accessor } .constructor === Object`
629
+ condition = 'value && typeof value === \ 'object\ ' && value .constructor === Object'
627
630
break
628
631
case 'array' :
629
- condition = ` Array.isArray(obj ${ accessor } )`
632
+ condition = ' Array.isArray(value)'
630
633
break
631
634
default :
632
635
if ( Array . isArray ( type ) ) {
0 commit comments