@@ -372,12 +372,14 @@ function buildInnerObject (context, location) {
372372 code += 'let json = JSON_STR_BEGIN_OBJECT\n'
373373
374374 let addComma = ''
375- if ( ! hasRequiredProperties ) {
375+ const needComma = ! hasRequiredProperties && ( propertiesKeys . size > 1 || ( schema . patternProperties || schema . additionalProperties !== false ) )
376+ if ( needComma ) {
376377 code += 'let addComma = false\n'
377378 addComma = '!addComma && (addComma = true) || (json += JSON_STR_COMMA)'
378379 }
379380
380381 let counterValue = 0
382+ let i = 0
381383 for ( const key of propertiesKeys ) {
382384 let propertyLocation = propertiesLocation . getPropertyLocation ( key )
383385 if ( propertyLocation . schema . $ref ) {
@@ -399,7 +401,7 @@ function buildInnerObject (context, location) {
399401
400402 if ( defaultValue !== undefined ) {
401403 code += ` else {
402- ${ addComma }
404+ ${ i > 0 ? addComma : ( needComma ? 'addComma = true' : '' ) }
403405 json += ${ JSON . stringify ( sanitizedKey + ':' + JSON . stringify ( defaultValue ) ) }
404406 }
405407 `
@@ -415,6 +417,8 @@ function buildInnerObject (context, location) {
415417 if ( hasRequiredProperties ) {
416418 addComma = 'json += \',\''
417419 }
420+
421+ i ++
418422 }
419423
420424 if ( schema . patternProperties || schema . additionalProperties ) {
@@ -587,10 +591,10 @@ function buildArray (context, location) {
587591 functionCode += `
588592 if (${ i } < arrayLength) {
589593 if (${ buildArrayTypeCondition ( item . type , value ) } ) {
590- ${ tmpRes }
591- if (${ i } < arrayEnd) {
594+ if (${ i } ) {
592595 json += JSON_STR_COMMA
593596 }
597+ ${ tmpRes }
594598 } else {
595599 throw new Error(\`Item at ${ i } does not match schema definition.\`)
596600 }
@@ -601,21 +605,21 @@ function buildArray (context, location) {
601605 if ( schema . additionalItems ) {
602606 functionCode += `
603607 for (let i = ${ itemsSchema . length } ; i < arrayLength; i++) {
604- json += JSON.stringify(obj[i])
605- if (i < arrayEnd) {
608+ if (i) {
606609 json += JSON_STR_COMMA
607610 }
611+ json += JSON.stringify(obj[i])
608612 }`
609613 }
610614 } else {
611615 const code = buildValue ( context , itemsLocation , 'value' )
612616 functionCode += `
613617 for (let i = 0; i < arrayLength; i++) {
614- const value = obj[i]
615- ${ code }
616- if (i < arrayEnd) {
618+ if (i) {
617619 json += JSON_STR_COMMA
618620 }
621+ const value = obj[i]
622+ ${ code }
619623 }`
620624 }
621625
0 commit comments