Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,14 @@ function buildInnerObject (context, location) {
code += 'let json = JSON_STR_BEGIN_OBJECT\n'

let addComma = ''
if (!hasRequiredProperties) {
const needComma = !hasRequiredProperties && (propertiesKeys.size > 1 || (schema.patternProperties || schema.additionalProperties !== false))
if (needComma) {
code += 'let addComma = false\n'
addComma = '!addComma && (addComma = true) || (json += JSON_STR_COMMA)'
}

let counterValue = 0
let i = 0
for (const key of propertiesKeys) {
let propertyLocation = propertiesLocation.getPropertyLocation(key)
if (propertyLocation.schema.$ref) {
Expand All @@ -399,7 +401,7 @@ function buildInnerObject (context, location) {

if (defaultValue !== undefined) {
code += ` else {
${addComma}
${i > 0 ? addComma : (needComma ? 'addComma = true' : '')}
json += ${JSON.stringify(sanitizedKey + ':' + JSON.stringify(defaultValue))}
}
`
Expand All @@ -415,6 +417,8 @@ function buildInnerObject (context, location) {
if (hasRequiredProperties) {
addComma = 'json += \',\''
}

i++
}

if (schema.patternProperties || schema.additionalProperties) {
Expand Down Expand Up @@ -587,10 +591,10 @@ function buildArray (context, location) {
functionCode += `
if (${i} < arrayLength) {
if (${buildArrayTypeCondition(item.type, value)}) {
${tmpRes}
if (${i} < arrayEnd) {
if (${i}) {
json += JSON_STR_COMMA
}
${tmpRes}
} else {
throw new Error(\`Item at ${i} does not match schema definition.\`)
}
Expand All @@ -601,21 +605,21 @@ function buildArray (context, location) {
if (schema.additionalItems) {
functionCode += `
for (let i = ${itemsSchema.length}; i < arrayLength; i++) {
json += JSON.stringify(obj[i])
if (i < arrayEnd) {
if (i) {
json += JSON_STR_COMMA
}
json += JSON.stringify(obj[i])
}`
}
} else {
const code = buildValue(context, itemsLocation, 'value')
functionCode += `
for (let i = 0; i < arrayLength; i++) {
const value = obj[i]
${code}
if (i < arrayEnd) {
if (i) {
json += JSON_STR_COMMA
}
const value = obj[i]
${code}
}`
}

Expand Down
57 changes: 57 additions & 0 deletions test/json-add-comma.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict'

const { test } = require('node:test')
const build = require('..')

test('additionalProperties: false', (t) => {
t.plan(1)
const stringify = build({
title: 'additionalProperties',
type: 'object',
properties: {
foo: {
type: 'string'
}
},
additionalProperties: false
})

const obj = { foo: 'a', bar: 'b', baz: 'c' }
t.assert.equal(stringify(obj), '{"foo":"a"}')
})

test('additionalProperties: {}', (t) => {
t.plan(1)
const stringify = build({
title: 'additionalProperties',
type: 'object',
properties: {
foo: {
type: 'string'
}
},
additionalProperties: {}
})

const obj = { foo: 'a', bar: 'b', baz: 'c' }
t.assert.equal(stringify(obj), '{"foo":"a","bar":"b","baz":"c"}')
})

test('additionalProperties: {type: string}', (t) => {
t.plan(1)
const stringify = build({
title: 'additionalProperties',
type: 'object',
properties: {
foo: {
type: 'string'
}
},
additionalProperties: {
type: 'string'
}
})

const obj = { foo: 'a', bar: 'b', baz: 'c' }
t.assert.equal(stringify(obj), '{"foo":"a","bar":"b","baz":"c"}')
})