From e61fb2cf04de817485483cf27a24a789ef2c6441 Mon Sep 17 00:00:00 2001 From: Kyle Upton Date: Tue, 13 Feb 2024 16:47:28 -0500 Subject: [PATCH 1/2] fix: coerce undefined to string (#680) --- lib/serializer.js | 2 +- test/array.test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/serializer.js b/lib/serializer.js index 922be357..bea1979e 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 || str === undefined) { 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 = { From 92fe231acdce57b6fd7430c28f9cc61e887caacb Mon Sep 17 00:00:00 2001 From: Kyle Upton <44348980+kyleaupton@users.noreply.github.com> Date: Wed, 14 Feb 2024 10:24:49 -0500 Subject: [PATCH 2/2] Update lib/serializer.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Gürgün Dayıoğlu Signed-off-by: Kyle Upton <44348980+kyleaupton@users.noreply.github.com> --- lib/serializer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/serializer.js b/lib/serializer.js index bea1979e..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 || str === undefined) { + if (str == null) { return '""' } if (str instanceof Date) {