Skip to content

Commit a8c7907

Browse files
authored
Checks if key value is undefined in isEmpty function (#260)
1 parent b592482 commit a8c7907

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,9 @@ function nested (laterCode, name, key, location, subKey, isArray) {
12401240

12411241
function isEmpty (schema) {
12421242
for (var key in schema) {
1243-
if (schema.hasOwnProperty(key)) return false
1243+
if (schema.hasOwnProperty(key) && schema[key] !== undefined) {
1244+
return false
1245+
}
12441246
}
12451247
return true
12461248
}

test/any.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ test('object with nested random property', (t) => {
3333
}), '{"id":1,"name":["first","last"]}')
3434
})
3535

36+
// reference: https://github.com/fastify/fast-json-stringify/issues/259
37+
test('object with empty schema with $id: undefined set', (t) => {
38+
t.plan(1)
39+
40+
const schema = {
41+
title: 'empty schema to allow any object with $id: undefined set',
42+
type: 'object',
43+
properties: {
44+
name: { $id: undefined }
45+
}
46+
}
47+
const stringify = build(schema)
48+
t.is(stringify({
49+
name: 'string'
50+
}), '{"name":"string"}')
51+
})
52+
3653
test('array with random items', (t) => {
3754
t.plan(1)
3855

0 commit comments

Comments
 (0)