Skip to content

Commit 929320b

Browse files
committed
Updated docs and example
1 parent 14ec27b commit 929320b

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,45 @@ const obj = {
152152
}
153153

154154
console.log(stringify(obj)) // '{"nickname":"nick","matchfoo":"42","otherfoo":"str","matchnum":3}'
155+
<a name="additionalProperties"></a>
156+
#### Additional properties
157+
`fast-json-stringify` supports additional properties as defined inside JSON schema.
158+
*additionalProperties* must be an object or a boolean, declared in this way: `{ type: 'type' }`.
159+
*additionalProperties* will work only for the properties that are not explicitly listed in the *properties* and *patternProperties* objects.
160+
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>.
161+
Example:
162+
```javascript
163+
const stringify = fastJson({
164+
title: 'Example Schema',
165+
type: 'object',
166+
properties: {
167+
nickname: {
168+
type: 'string'
169+
}
170+
},
171+
patternProperties: {
172+
'num': {
173+
type: 'number'
174+
},
175+
'.*foo$': {
176+
type: 'string'
177+
}
178+
},
179+
additionalProperties: {
180+
type: 'string'
181+
}
182+
})
183+
184+
const obj = {
185+
nickname: 'nick',
186+
matchfoo: 42,
187+
otherfoo: 'str'
188+
matchnum: 3,
189+
nomatchstr: 'valar morghulis',
190+
nomatchint: 313
191+
}
192+
193+
console.log(stringify(obj)) // '{"matchfoo":"42","otherfoo":"str","matchnum":3,"nomatchstr":"valar morghulis",nomatchint:"313","nickname":"nick"}'
155194
```
156195

157196
## Acknowledgements

example.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ const stringify = fastJson({
4949
'test': {
5050
type: 'number'
5151
}
52+
},
53+
additionalProperties: {
54+
type: 'string'
5255
}
5356
})
5457

@@ -63,5 +66,8 @@ console.log(stringify({
6366
test: 42,
6467
strtest: '23',
6568
arr: [{ str: 'stark' }, { str: 'lannister' }],
66-
obj: { bool: true }
69+
obj: { bool: true },
70+
notmatch: 'valar morghulis',
71+
notmatchobj: { a: true },
72+
notmatchnum: 42
6773
}))

0 commit comments

Comments
 (0)