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
30 changes: 30 additions & 0 deletions benchmark/bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
42 changes: 42 additions & 0 deletions test/additionalProperties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Loading