Skip to content

Commit b750f01

Browse files
allevodelvedor
authored andcommitted
Doc if/then/else (#92)
1 parent e47da97 commit b750f01

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,51 @@ const stringify = fastJson({
286286
}
287287
```
288288
289-
This schema will accept a string or a boolean for the property `undecidedType`. If no schema matches the data, it will be stringified as `null`.
289+
<a name="if-then-else"></a>
290+
#### If/then/else
291+
`fast-json-stringify` supports `if/then/else` jsonschema feature. See [ajv documentation](https://ajv.js.org/keywords.html#ifthenelse).
292+
293+
Example:
294+
```javascript
295+
const stringify = fastJson({
296+
'type': 'object',
297+
'properties': {
298+
'kind': { 'type': 'string', 'enum': ['foobar', 'greeting'] }
299+
},
300+
'if': {
301+
'properties': {
302+
'kind': { 'type': 'string', 'enum': ['foobar'] }
303+
}
304+
},
305+
'then': {
306+
'properties': {
307+
'foo': { 'type': 'string' },
308+
'bar': { 'type': 'number' }
309+
}
310+
},
311+
'else': {
312+
'properties': {
313+
'hi': { 'type': 'string' },
314+
'hello': { 'type': 'number' }
315+
}
316+
}
317+
})
318+
319+
console.log(stringify({
320+
kind: 'greeting',
321+
foo: 'FOO',
322+
bar: 42,
323+
hi: 'HI',
324+
hello: 45
325+
})) // {"kind":"greeting","hi":"HI","hello":45}
326+
console.log(stringify({
327+
kind: 'foobar',
328+
foo: 'FOO',
329+
bar: 42,
330+
hi: 'HI',
331+
hello: 45
332+
})) // {"kind":"greeting","foo":"FOO","bar":42}
333+
```
290334
291335
<a name="ref"></a>
292336
#### Reuse - $ref

0 commit comments

Comments
 (0)