Skip to content

Commit 6c6e6dc

Browse files
authored
Merge pull request #41 from fastify/serialize-null-as-string
Handle null when value should be string
2 parents e9dc0d9 + be2c364 commit 6c6e6dc

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ function $asBoolean (bool) {
135135
function $asString (str) {
136136
if (str instanceof Date) {
137137
return '"' + str.toISOString() + '"'
138+
} else if (str === null) {
139+
return '""'
138140
} else if (str instanceof RegExp) {
139141
str = str.source
140142
} else if (typeof str !== 'string') {

test/missing-values.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,18 @@ test('missing values', (t) => {
2626
t.equal('{"str":"string","val":"value"}', stringify({ str: 'string', val: 'value' }))
2727
t.equal('{"str":"string","num":42,"val":"value"}', stringify({ str: 'string', num: 42, val: 'value' }))
2828
})
29+
30+
test('handle null when value should be string', (t) => {
31+
t.plan(1)
32+
33+
const stringify = build({
34+
type: 'object',
35+
properties: {
36+
str: {
37+
type: 'string'
38+
}
39+
}
40+
})
41+
42+
t.equal('{"str":""}', stringify({ str: null }))
43+
})

0 commit comments

Comments
 (0)