Skip to content

Commit

Permalink
Add schemaPath to README
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian de Bhal committed Dec 5, 2017
1 parent ddb520e commit 1d8334c
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,15 @@ var doc = {hello: 'world', notInSchema: true}
console.log(filter(doc)) // {hello: 'world'}
```

## Verbose mode outputs the value on errors
## Verbose mode shows more information about the source of the error

is-my-json-valid outputs the value causing an error when verbose is set to true
When the `verbose` options is set to `true`, `is-my-json-valid` also outputs:

- `value`: The data value that caused the error
- `schemaPath`: an array of keys indicating which sub-schema failed

``` js
var validate = validator({
var schema = {
required: true,
type: 'object',
properties: {
Expand All @@ -123,12 +126,33 @@ var validate = validator({
type: 'string'
}
}
}, {
}
var validate = validator(schema, {
verbose: true
})

validate({hello: 100});
console.log(validate.errors) // {field: 'data.hello', message: 'is the wrong type', value: 100, type: 'string'}
console.log(validate.errors, null, 2)
// [ { field: 'data.hello',
// message: 'is the wrong type',
// value: 100,
// type: 'string',
// schemaPath: [ 'properties', 'hello' ] } ]
```

Many popular libraries make it easy to retrieve the failing rule with the `schemaPath`:
```
var schemaPath = validate.errors[0].schemaPath
var R = require('ramda')
console.log( 'All evaluate to the same thing: ', R.equals(
schema.properties.hello,
{ required: true, type: 'string' },
R.path(schemaPath, schema),
require('lodash').get(schema, schemaPath),
require('jsonpointer').get(schema, [""].concat(schemaPath))
))
// All evaluate to the same thing: true
```

## Greedy mode tries to validate as much as possible
Expand Down

0 comments on commit 1d8334c

Please sign in to comment.