diff --git a/index.js b/index.js index e56b3119..7310ebff 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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))} } ` @@ -415,6 +417,8 @@ function buildInnerObject (context, location) { if (hasRequiredProperties) { addComma = 'json += \',\'' } + + i++ } if (schema.patternProperties || schema.additionalProperties) { @@ -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.\`) } @@ -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} }` } diff --git a/test/json-add-comma.test.js b/test/json-add-comma.test.js new file mode 100644 index 00000000..82818848 --- /dev/null +++ b/test/json-add-comma.test.js @@ -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"}') +})