fast-json-stringify
Advanced tools
Comparing version 0.5.0 to 0.6.0
28
index.js
@@ -143,5 +143,8 @@ 'use strict' | ||
Object.keys(schema.properties).forEach((key, i, a) => { | ||
// Using obj.key !== undefined instead of obj.hasOwnProperty(prop) for perf reasons, | ||
// see https://github.com/mcollina/fast-json-stringify/pull/3 for discussion. | ||
code += ` | ||
json += '${$asString(key)}:' | ||
` | ||
if (obj.${key} !== undefined) { | ||
json += '${$asString(key)}:' | ||
` | ||
@@ -154,4 +157,17 @@ const result = nested(laterCode, name, '.' + key, schema.properties[key]) | ||
if (i < a.length - 1) { | ||
code += 'json += \',\'' | ||
code += ` | ||
json += \',\' | ||
` | ||
} | ||
if (schema.properties[key].required) { | ||
code += ` | ||
} else { | ||
throw new Error('${key} is required!') | ||
` | ||
} | ||
code += ` | ||
} | ||
` | ||
}) | ||
@@ -207,8 +223,2 @@ | ||
var funcName | ||
if (schema.required) { | ||
code += ` | ||
if (!obj.hasOwnProperty('${key.slice(1)}')) { | ||
throw new Error('${key} is required!') | ||
}` | ||
} | ||
const type = schema.type | ||
@@ -215,0 +225,0 @@ switch (type) { |
{ | ||
"name": "fast-json-stringify", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Stringify your JSON at max speed", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -70,10 +70,54 @@ # fast-json-stringify [![Build Status](https://travis-ci.org/mcollina/fast-json-stringify.svg)](https://travis-ci.org/mcollina/fast-json-stringify) | ||
*Specific use cases:* | ||
#### Specific use cases | ||
| Instance | Serialized as | | ||
| -----------|---------------------------------------------| | ||
| `Date` | `string` <small>via `toISOString()`</small> | | ||
| `Date` | `string` via `toISOString()` | | ||
| `RegExp` | `string` | | ||
#### Required | ||
You can set specific fields of an object as `required` in your schema, by adding `required: true` inside the key properties. | ||
Example: | ||
```javascript | ||
const schema = { | ||
title: 'Example Schema with required field', | ||
type: 'object', | ||
properties: { | ||
nickname: { | ||
type: 'string' | ||
}, | ||
mail: { | ||
type: 'string', | ||
required: true | ||
} | ||
} | ||
} | ||
``` | ||
If the object to stringify has not the required field(s), `fast-json-stringify` will throw an error. | ||
#### Missing fields | ||
If a field *is present* in the schema (and is not required) but it *is not present* in the object to stringify, `fast-json-stringify` will not write it in the final string. | ||
Example: | ||
```javascript | ||
const stringify = fastJson({ | ||
title: 'Example Schema', | ||
type: 'object', | ||
properties: { | ||
nickname: { | ||
type: 'string' | ||
}, | ||
mail: { | ||
type: 'string', | ||
required: true | ||
} | ||
} | ||
}) | ||
const obj = { | ||
mail: 'mail@example.com' | ||
} | ||
console.log(stringify(obj)) // '{"mail":"mail@example.com"}' | ||
``` | ||
## Acknowledgements | ||
@@ -80,0 +124,0 @@ |
26
test.js
@@ -291,5 +291,29 @@ 'use strict' | ||
} catch (e) { | ||
t.is(e.message, '.str is required!') | ||
t.is(e.message, 'str is required!') | ||
t.pass() | ||
} | ||
}) | ||
test('missing values', (t) => { | ||
t.plan(3) | ||
const stringify = build({ | ||
title: 'object with missing values', | ||
type: 'object', | ||
properties: { | ||
str: { | ||
type: 'string' | ||
}, | ||
num: { | ||
type: 'number' | ||
}, | ||
val: { | ||
type: 'string' | ||
} | ||
} | ||
}) | ||
t.equal('{"val":"value"}', stringify({ val: 'value' })) | ||
t.equal('{"str":"string","val":"value"}', stringify({ str: 'string', val: 'value' })) | ||
t.equal('{"str":"string","num":42,"val":"value"}', stringify({ str: 'string', num: 42, val: 'value' })) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18376
652
129