Skip to content

Commit

Permalink
Updated docs and example
Browse files Browse the repository at this point in the history
  • Loading branch information
delvedor committed Oct 4, 2016
1 parent 14ec27b commit 929320b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
39 changes: 39 additions & 0 deletions README.md
Expand Up @@ -152,6 +152,45 @@ const obj = {
}

console.log(stringify(obj)) // '{"nickname":"nick","matchfoo":"42","otherfoo":"str","matchnum":3}'
<a name="additionalProperties"></a>
#### Additional properties
`fast-json-stringify` supports additional properties as defined inside JSON schema.
*additionalProperties* must be an object or a boolean, declared in this way: `{ type: 'type' }`.
*additionalProperties* will work only for the properties that are not explicitly listed in the *properties* and *patternProperties* objects.
If *additionalProperties* is not present or is setted to false, every property that is not explicitly listed in the *properties* and *patternProperties* objects, will be ignored, as said in <a href="missingFields">Missing fields</a>.
Example:
```javascript
const stringify = fastJson({
title: 'Example Schema',
type: 'object',
properties: {
nickname: {
type: 'string'
}
},
patternProperties: {
'num': {
type: 'number'
},
'.*foo$': {
type: 'string'
}
},
additionalProperties: {
type: 'string'
}
})
const obj = {
nickname: 'nick',
matchfoo: 42,
otherfoo: 'str'
matchnum: 3,
nomatchstr: 'valar morghulis',
nomatchint: 313
}
console.log(stringify(obj)) // '{"matchfoo":"42","otherfoo":"str","matchnum":3,"nomatchstr":"valar morghulis",nomatchint:"313","nickname":"nick"}'
```

## Acknowledgements
Expand Down
8 changes: 7 additions & 1 deletion example.js
Expand Up @@ -49,6 +49,9 @@ const stringify = fastJson({
'test': {
type: 'number'
}
},
additionalProperties: {
type: 'string'
}
})

Expand All @@ -63,5 +66,8 @@ console.log(stringify({
test: 42,
strtest: '23',
arr: [{ str: 'stark' }, { str: 'lannister' }],
obj: { bool: true }
obj: { bool: true },
notmatch: 'valar morghulis',
notmatchobj: { a: true },
notmatchnum: 42
}))

0 comments on commit 929320b

Please sign in to comment.