Skip to content

Commit 81a799a

Browse files
committed
doc: more examples of parser API
1 parent e565d74 commit 81a799a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,30 @@ console.log(validate.errors)
166166
// [ { keywordLocation: '#/properties/hello/type', instanceLocation: '#/hello' } ]
167167
```
168168

169+
Or, similarly, with parser API:
170+
171+
```js
172+
const schema = {
173+
$schema: 'https://json-schema.org/draft/2019-09/schema',
174+
type: 'object',
175+
required: ['hello'],
176+
properties: {
177+
hello: {
178+
type: 'string',
179+
pattern: '^[a-z]+$',
180+
}
181+
},
182+
additionalProperties: false,
183+
}
184+
const parse = parser(schema, { includeErrors: true })
185+
186+
console.log(parse('{ "hello": 100 }'));
187+
// { valid: false,
188+
// error: 'JSON validation failed for type at #/hello',
189+
// errors: [ { keywordLocation: '#/properties/hello/type', instanceLocation: '#/hello' } ]
190+
// }
191+
```
192+
169193
Only the first error is reported by default unless `allErrors` option is also set to `true` in
170194
addition to `includeErrors`.
171195

0 commit comments

Comments
 (0)