diff --git a/benchmark/bench.js b/benchmark/bench.js index 96d3587c..ee148d37 100644 --- a/benchmark/bench.js +++ b/benchmark/bench.js @@ -277,6 +277,36 @@ const benchmarks = [ b5: true } }, + { + name: 'object with additionalProperties', + schema: { + type: 'object', + properties: { + id: { type: 'integer' } + }, + additionalProperties: { type: 'string' } + }, + input: { id: 1, k0: 'v0', k1: 'v1', k2: 'v2', k3: 'v3', k4: 'v4', k5: 'v5', k6: 'v6', k7: 'v7', k8: 'v8' } + }, + { + name: 'object with additionalProperties: true', + schema: { + type: 'object', + additionalProperties: true + }, + input: { a: 1, b: 'two', c: true, d: null, e: 3.5, f: [1, 2], g: { h: 1 }, i: 'str', j: false, k: 42 } + }, + { + name: 'object with patternProperties', + schema: { + type: 'object', + patternProperties: { + '^s_': { type: 'string' }, + '^n_': { type: 'number' } + } + }, + input: { s_0: 'a', s_1: 'b', s_2: 'c', s_3: 'd', s_4: 'e', n_0: 0, n_1: 1.5, n_2: 3, n_3: 4.5, n_4: 6 } + }, { name: 'simple object', schema: { diff --git a/index.js b/index.js index 69870a8b..26dea608 100644 --- a/index.js +++ b/index.js @@ -712,13 +712,13 @@ function buildExtraObjectPropertiesSerializer (context, location, addComma, objV let code = ` for (const key of Object.keys(${objVar})) { + ${propertiesKeys.length > 0 ? `if (${propertiesKeys.map(k => `key === ${JSON.stringify(k)}`).join(' || ')}) continue` : ''} + const value = ${objVar}[key] if ( - ${propertiesKeys.length > 0 ? propertiesKeys.map(k => `key === ${JSON.stringify(k)}`).join(' || ') + ' ||' : ''} - ${objVar}[key] === undefined || - typeof ${objVar}[key] === 'function' || - typeof ${objVar}[key] === 'symbol' + value === undefined || + typeof value === 'function' || + typeof value === 'symbol' ) continue - const value = ${objVar}[key] ` const patternPropertiesLocation = location.getPropertyLocation('patternProperties') diff --git a/test/additionalProperties.test.js b/test/additionalProperties.test.js index 980dd00b..9f6fe405 100644 --- a/test/additionalProperties.test.js +++ b/test/additionalProperties.test.js @@ -417,3 +417,45 @@ test('additionalProperties set to false without declared properties ignores unma t.assert.equal(out, '{"str1":"a"}') t.assert.deepStrictEqual(JSON.parse(out), { str1: 'a' }) }) + +test('extra properties skip undefined, function and symbol values and keep key order', (t) => { + const input = { 2: 'two', id: 1, a: 'x', u: undefined, f () {}, s: Symbol('s'), n_1: 3, 1: 'one', z: null } + const schemas = [ + [{ type: 'object', properties: { id: { type: 'integer' } }, additionalProperties: { type: 'string' } }, + '{"id":1,"1":"one","2":"two","a":"x","n_1":"3","z":""}'], + [{ type: 'object', additionalProperties: { type: 'string' } }, + '{"1":"one","2":"two","id":"1","a":"x","n_1":"3","z":""}'], + [{ type: 'object', properties: { id: { type: 'integer' } }, additionalProperties: true }, + '{"id":1,"1":"one","2":"two","a":"x","n_1":3,"z":null}'], + [{ type: 'object', properties: { id: { type: 'integer' } }, patternProperties: { '^n_': { type: 'number' } }, additionalProperties: { type: 'string' } }, + '{"id":1,"1":"one","2":"two","a":"x","n_1":3,"z":""}'], + [{ type: 'object', patternProperties: { '^n_': { type: 'number' } }, additionalProperties: false }, + '{"n_1":3}'] + ] + t.plan(schemas.length) + + for (const [schema, expected] of schemas) { + t.assert.equal(build(schema)(input), expected) + } +}) + +test('extra properties read each value once', (t) => { + const schemas = [ + { type: 'object', properties: { id: { type: 'integer' } }, additionalProperties: { type: 'string' } }, + { type: 'object', additionalProperties: true }, + { type: 'object', patternProperties: { '^g': { type: 'string' } } } + ] + t.plan(schemas.length * 2) + + for (const schema of schemas) { + let reads = 0 + const input = { + get g () { + reads++ + return reads === 1 ? 'first' : () => {} + } + } + t.assert.equal(build(schema)(input), '{"g":"first"}') + t.assert.equal(reads, 1) + } +})