Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ fast-json-stringify-uglified obj x 5,331,581 ops/sec ±0.73% (91 runs sampled)
- <a href="#ref">`Reuse - $ref`</a>
- <a href="#long">`Long integers`</a>
- <a href="#uglify">`Uglify`</a>
- <a href="#nullable">`Nullable`</a>
- <a href="#acknowledgements">`Acknowledgements`</a>
- <a href="#license">`License`</a>

Expand Down Expand Up @@ -462,6 +463,35 @@ const stringify = fastJson({
console.log(stringify({ some: 'object' })) // '{"some":"object"}'
```

<a name="nullable"></a>
#### Nullable

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`.

##### Nullable object
```javascript
const stringify = fastJson({
'title': 'Nullable schema',
'type': 'object',
'nullable': true,
'properties': {
'product': {
'nullable': true,
'type': 'object',
'properties': {
'name': {
'type': 'string'
}
}
}
}
})

console.log(stringify({product: {name: "hello"}})) // "{"product":{"name":"hello"}}"
console.log(stringify({product: null})) // "{"product":null}"
console.log(stringify(null)) // null
```

<a name="acknowledgements"></a>
## Acknowledgements

Expand Down
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,15 @@ function toJSON (variableName) {
function buildObject (schema, code, name, externalSchema, fullSchema) {
code += `
function ${name} (input) {
`
if (schema.nullable) {
code += `
if(input === null) {
return '${$asNull()}';
}
`
}
code += `
var obj = ${toJSON('input')}
var json = '{'
var addComma = false
Expand Down
91 changes: 91 additions & 0 deletions test/toJSON.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,94 @@ test('not use toJSON if does not exist', (t) => {

t.equal('{"product":{"name":"cola"}}', stringify(object))
})

test('not fail on null object declared nullable', (t) => {
t.plan(1)

const stringify = build({
title: 'simple object',
type: 'object',
nullable: true,
properties: {
product: {
type: 'object',
properties: {
name: {
type: 'string'
}
}
}
}
})
t.equal('null', stringify(null))
})

test('not fail on null sub-object declared nullable', (t) => {
t.plan(1)

const stringify = build({
title: 'simple object',
type: 'object',
properties: {
product: {
nullable: true,
type: 'object',
properties: {
name: {
type: 'string'
}
}
}
}
})
const object = {
product: null
}
t.equal('{"product":null}', stringify(object))
})

test('throw an error on non nullable null sub-object', (t) => {
t.plan(1)

const stringify = build({
title: 'simple object',
type: 'object',
properties: {
product: {
nullable: false,
type: 'object',
properties: {
name: {
type: 'string'
}
}
}
}
})
const object = {
product: null
}
t.throws(() => { stringify(object) })
})

test('throw an error on non nullable null object', (t) => {
t.plan(1)

const stringify = build({
title: 'simple object',
nullable: false,
type: 'object',
properties: {
product: {
nullable: false,
type: 'object',
properties: {
name: {
type: 'string'
}
}
}
}
})
t.throws(() => { stringify(null) })
})