fast-json-stringify
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -456,5 +456,12 @@ 'use strict' | ||
if (schema.required && schema.required.indexOf(key) !== -1) { | ||
var defaultValue = schema.properties[key].default | ||
if (defaultValue !== undefined) { | ||
code += ` | ||
} else { | ||
${addComma} | ||
json += '${$asString(key)}:${JSON.stringify(defaultValue).replace(/'/g, '\'')}' | ||
` | ||
} else if (schema.required && schema.required.indexOf(key) !== -1) { | ||
code += ` | ||
} else { | ||
throw new Error('${key} is required!') | ||
@@ -461,0 +468,0 @@ ` |
{ | ||
"name": "fast-json-stringify", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Stringify your JSON at max speed", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -166,2 +166,24 @@ # fast-json-stringify [![Build Status](https://travis-ci.org/fastify/fast-json-stringify.svg?branch=master)](https://travis-ci.org/fastify/fast-json-stringify) | ||
<a name="defaults"></a> | ||
#### Defaults | ||
`fast-json-stringify` supports `default` jsonschema key in order to serialize a value | ||
if it is `undefined` or not present. | ||
Example: | ||
```javascript | ||
const stringify = fastJson({ | ||
title: 'Example Schema', | ||
type: 'object', | ||
properties: { | ||
nickname: { | ||
type: 'string', | ||
default: 'the default string' | ||
} | ||
} | ||
}) | ||
console.log(stringify({})) // '{"nickname":"the default string"}' | ||
console.log(stringify({nickname: 'my-nickname'})) // '{"nickname":"my-nickname"}' | ||
``` | ||
<a name="patternProperties"></a> | ||
@@ -168,0 +190,0 @@ #### Pattern properties |
82340
25
3053
427