Skip to content

Commit cf61b36

Browse files
authored
Merge pull request #52 from JbIPS/master
Adds anyOf documentation
2 parents 6145b8c + ff64434 commit cf61b36

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ fast-json-stringify-uglified obj x 5,902,021 ops/sec ±1.06% (91 runs sampled)
4949
- <a href="#missingFields">`Missing fields`</a>
5050
- <a href="#patternProperties">`Pattern Properties`</a>
5151
- <a href="#additionalProperties">`Additional Properties`</a>
52+
- <a href="#anyof">`AnyOf`</a>
5253
- <a href="#ref">`Reuse - $ref`</a>
5354
- <a href="#long">`Long integers`</a>
5455
- <a href="#uglify">`Uglify`</a>
@@ -240,6 +241,31 @@ const obj = {
240241
console.log(stringify(obj)) // '{"matchfoo":"42","otherfoo":"str","matchnum":3,"nomatchstr":"valar morghulis",nomatchint:"313","nickname":"nick"}'
241242
```
242243

244+
#### AnyOf
245+
246+
`fast-json-stringify` supports anyOf keyword as defined inside JSON schema. *anyOf* must be an array of valid JSON schemas. The differents schemas will be tried in this order so `stringify` will be slower the farest the right type is from the start of the array.
247+
248+
*anyOf* uses [ajv](https://www.npmjs.com/package/ajv) as a JSON schema validator to find the schema that match the data and this has an impact on performances. Use it only as a last resort.
249+
250+
Example:
251+
```javascript
252+
const stringify = fastJson({
253+
title: 'Example Schema',
254+
type: 'object',
255+
properties: {
256+
'undecidedType': {
257+
'anyOf': [{
258+
type: 'string'
259+
}, {
260+
type: 'boolean'
261+
}]
262+
}
263+
}
264+
}
265+
```
266+
267+
This schema will accept a string or a boolean for the property `undecidedType`. If no schema match the data, it will be stringified as `null`.
268+
243269
<a name="ref"></a>
244270
#### Reuse - $ref
245271
If you want to reuse a definition of a value, you can use the property `$ref`.

0 commit comments

Comments
 (0)