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
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ And nested ones, too.
| `Date` | `string` via `toISOString()` |
| `RegExp` | `string` |

#### Required
#### Required
You can set specific fields of an object as `required` in your schema, by adding `required: true` inside the key properties.
Example:
```javascript
Expand All @@ -94,6 +94,31 @@ const schema = {
```
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

This project was kindly sponsored by [nearForm](http://nearform.com).
Expand Down
28 changes: 19 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,34 @@ function buildObject (schema, code, name) {
var laterCode = ''

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)}:'
`

const result = nested(laterCode, name, '.' + key, schema.properties[key])

code += result.code
laterCode = result.laterCode

if (i < a.length - 1) {
code += 'json += \',\''
code += `
json += \',\'
`
}

if (schema.properties[key].required) {
code += `
} else {
throw new Error('${key} is required!')
`
}

code += `
}
`
})

code += `
Expand Down Expand Up @@ -202,12 +218,6 @@ function buildArray (schema, code, name) {
function nested (laterCode, name, key, schema) {
var code = ''
var funcName
if (schema.required) {
code += `
if (!obj.hasOwnProperty('${key.slice(1)}')) {
throw new Error('${key} is required!')
}`
}
const type = schema.type
switch (type) {
case 'null':
Expand Down
26 changes: 25 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,31 @@ test('object with required field', (t) => {
})
t.fail()
} 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' }))
})