diff --git a/lib/serializer.js b/lib/serializer.js index 922be357..21e06729 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -100,7 +100,7 @@ module.exports = class Serializer { asString (str) { if (typeof str !== 'string') { - if (str === null) { + if (str == null) { return '""' } if (str instanceof Date) { diff --git a/test/array.test.js b/test/array.test.js index fbffe02f..c9bbc671 100644 --- a/test/array.test.js +++ b/test/array.test.js @@ -195,6 +195,34 @@ buildTest({ '@data': ['test'] }) +test('array of strings with null and undefined elements coerced to empty string', (t) => { + t.plan(1) + + const schema = { + type: 'object', + properties: { + foo: { + type: 'array', + items: { + type: 'string' + } + } + } + } + + const stringify = build(schema) + const result = stringify({ + foo: [ + 'foo', + 'bar', + null, + undefined + ] + }) + + t.equal(result, '{"foo":["foo","bar","",""]}') +}) + test('invalid items throw', (t) => { t.plan(1) const schema = {