Skip to content

Commit 073b41e

Browse files
jtassinmcollina
authored andcommitted
Better support for null values (#130) (#131)
1 parent 68d58b4 commit 073b41e

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ fast-json-stringify-uglified obj x 5,331,581 ops/sec ±0.73% (91 runs sampled)
5353
- <a href="#ref">`Reuse - $ref`</a>
5454
- <a href="#long">`Long integers`</a>
5555
- <a href="#uglify">`Uglify`</a>
56+
- <a href="#nullable">`Nullable`</a>
5657
- <a href="#acknowledgements">`Acknowledgements`</a>
5758
- <a href="#license">`License`</a>
5859

@@ -462,6 +463,35 @@ const stringify = fastJson({
462463
console.log(stringify({ some: 'object' })) // '{"some":"object"}'
463464
```
464465
466+
<a name="nullable"></a>
467+
#### Nullable
468+
469+
According to the [Open API 3.0 specification](https://swagger.io/docs/specification/data-models/data-types/#null), a value that can be null must be declared `nullable`.
470+
471+
##### Nullable object
472+
```javascript
473+
const stringify = fastJson({
474+
'title': 'Nullable schema',
475+
'type': 'object',
476+
'nullable': true,
477+
'properties': {
478+
'product': {
479+
'nullable': true,
480+
'type': 'object',
481+
'properties': {
482+
'name': {
483+
'type': 'string'
484+
}
485+
}
486+
}
487+
}
488+
})
489+
490+
console.log(stringify({product: {name: "hello"}})) // "{"product":{"name":"hello"}}"
491+
console.log(stringify({product: null})) // "{"product":null}"
492+
console.log(stringify(null)) // null
493+
```
494+
465495
<a name="acknowledgements"></a>
466496
## Acknowledgements
467497

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,15 @@ function toJSON (variableName) {
621621
function buildObject (schema, code, name, externalSchema, fullSchema) {
622622
code += `
623623
function ${name} (input) {
624+
`
625+
if (schema.nullable) {
626+
code += `
627+
if(input === null) {
628+
return '${$asNull()}';
629+
}
630+
`
631+
}
632+
code += `
624633
var obj = ${toJSON('input')}
625634
var json = '{'
626635
var addComma = false

test/toJSON.test.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,94 @@ test('not use toJSON if does not exist', (t) => {
8181

8282
t.equal('{"product":{"name":"cola"}}', stringify(object))
8383
})
84+
85+
test('not fail on null object declared nullable', (t) => {
86+
t.plan(1)
87+
88+
const stringify = build({
89+
title: 'simple object',
90+
type: 'object',
91+
nullable: true,
92+
properties: {
93+
product: {
94+
type: 'object',
95+
properties: {
96+
name: {
97+
type: 'string'
98+
}
99+
}
100+
}
101+
}
102+
})
103+
t.equal('null', stringify(null))
104+
})
105+
106+
test('not fail on null sub-object declared nullable', (t) => {
107+
t.plan(1)
108+
109+
const stringify = build({
110+
title: 'simple object',
111+
type: 'object',
112+
properties: {
113+
product: {
114+
nullable: true,
115+
type: 'object',
116+
properties: {
117+
name: {
118+
type: 'string'
119+
}
120+
}
121+
}
122+
}
123+
})
124+
const object = {
125+
product: null
126+
}
127+
t.equal('{"product":null}', stringify(object))
128+
})
129+
130+
test('throw an error on non nullable null sub-object', (t) => {
131+
t.plan(1)
132+
133+
const stringify = build({
134+
title: 'simple object',
135+
type: 'object',
136+
properties: {
137+
product: {
138+
nullable: false,
139+
type: 'object',
140+
properties: {
141+
name: {
142+
type: 'string'
143+
}
144+
}
145+
}
146+
}
147+
})
148+
const object = {
149+
product: null
150+
}
151+
t.throws(() => { stringify(object) })
152+
})
153+
154+
test('throw an error on non nullable null object', (t) => {
155+
t.plan(1)
156+
157+
const stringify = build({
158+
title: 'simple object',
159+
nullable: false,
160+
type: 'object',
161+
properties: {
162+
product: {
163+
nullable: false,
164+
type: 'object',
165+
properties: {
166+
name: {
167+
type: 'string'
168+
}
169+
}
170+
}
171+
}
172+
})
173+
t.throws(() => { stringify(null) })
174+
})

0 commit comments

Comments
 (0)