is-my-json-valid
Advanced tools
Comparing version 2.16.1 to 2.17.0
37
index.js
@@ -140,3 +140,3 @@ var genobj = require('generate-object-property') | ||
var visit = function(name, node, reporter, filter) { | ||
var visit = function(name, node, reporter, filter, schemaPath) { | ||
var properties = node.properties | ||
@@ -161,3 +161,10 @@ var type = node.type | ||
if (verbose) { | ||
validate('validate.errors.push({field:%s,message:%s,value:%s,type:%s})', formatName(prop || name), JSON.stringify(msg), value || name, JSON.stringify(type)) | ||
validate( | ||
'validate.errors.push({field:%s,message:%s,value:%s,type:%s,schemaPath:%s})', | ||
formatName(prop || name), | ||
JSON.stringify(msg), | ||
value || name, | ||
JSON.stringify(type), | ||
JSON.stringify(schemaPath) | ||
) | ||
} else { | ||
@@ -204,3 +211,3 @@ validate('validate.errors.push({field:%s,message:%s})', formatName(prop || name), JSON.stringify(msg)) | ||
validate('for (var %s = %d; %s < %s.length; %s++) {', i, node.items.length, i, name, i) | ||
visit(name+'['+i+']', node.additionalItems, reporter, filter) | ||
visit(name+'['+i+']', node.additionalItems, reporter, filter, schemaPath.concat('additionalItems')) | ||
validate('}') | ||
@@ -284,3 +291,3 @@ } | ||
validate('if (%s !== undefined) {', genobj(name, key)) | ||
visit(name, deps, reporter, filter) | ||
visit(name, deps, reporter, filter, schemaPath.concat(['dependencies', key])) | ||
validate('}') | ||
@@ -319,3 +326,3 @@ } | ||
} else { | ||
visit(name+'['+keys+'['+i+']]', node.additionalProperties, reporter, filter) | ||
visit(name+'['+keys+'['+i+']]', node.additionalProperties, reporter, filter, schemaPath.concat(['additionalProperties'])) | ||
} | ||
@@ -351,3 +358,3 @@ | ||
validate('var %s = errors', prev) | ||
visit(name, node.not, false, filter) | ||
visit(name, node.not, false, filter, schemaPath.concat('not')) | ||
validate('if (%s === errors) {', prev) | ||
@@ -365,3 +372,3 @@ error('negative schema matches') | ||
validate('for (var %s = 0; %s < %s.length; %s++) {', i, i, name, i) | ||
visit(name+'['+i+']', node.items, reporter, filter) | ||
visit(name+'['+i+']', node.items, reporter, filter, schemaPath.concat('items')) | ||
validate('}') | ||
@@ -383,3 +390,3 @@ | ||
validate('if (%s.test(%s)) {', p, keys+'['+i+']') | ||
visit(name+'['+keys+'['+i+']]', node.patternProperties[key], reporter, filter) | ||
visit(name+'['+keys+'['+i+']]', node.patternProperties[key], reporter, filter, schemaPath.concat(['patternProperties', key])) | ||
validate('}') | ||
@@ -402,4 +409,4 @@ }) | ||
if (node.allOf) { | ||
node.allOf.forEach(function(sch) { | ||
visit(name, sch, reporter, filter) | ||
node.allOf.forEach(function(sch, key) { | ||
visit(name, sch, reporter, filter, schemaPath.concat(['allOf', key])) | ||
}) | ||
@@ -545,3 +552,9 @@ } | ||
visit(genobj(name, p), properties[p], reporter, filter) | ||
visit( | ||
genobj(name, p), | ||
properties[p], | ||
reporter, | ||
filter, | ||
schemaPath.concat(tuple ? p : ['properties', p]) | ||
) | ||
@@ -562,3 +575,3 @@ if (Array.isArray(type) && type.indexOf('null') !== -1) validate('}') | ||
visit('data', schema, reporter, opts && opts.filter) | ||
visit('data', schema, reporter, opts && opts.filter, []) | ||
@@ -565,0 +578,0 @@ validate |
{ | ||
"name": "is-my-json-valid", | ||
"version": "2.16.1", | ||
"version": "2.17.0", | ||
"description": "A JSONSchema validator that uses code generation to be extremely fast", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -112,8 +112,11 @@ # is-my-json-valid | ||
## 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, | ||
@@ -127,3 +130,4 @@ type: 'object', | ||
} | ||
}, { | ||
} | ||
var validate = validator(schema, { | ||
verbose: true | ||
@@ -133,5 +137,25 @@ }) | ||
validate({hello: 100}); | ||
console.log(validate.errors) // {field: 'data.hello', message: 'is the wrong type', value: 100, type: 'string'} | ||
console.log(validate.errors) | ||
// [ { 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 | ||
@@ -138,0 +162,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
112946
3523
225