jsonschema
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -65,5 +65,8 @@ 'use strict'; | ||
var throwError = options.throwError; | ||
var throwAll = options.throwAll; | ||
options.throwError = false; | ||
options.throwAll = false; | ||
var res = this.validateSchema(instance, schema, options, ctx); | ||
options.throwError = throwError; | ||
options.throwAll = throwAll; | ||
@@ -70,0 +73,0 @@ if (!res.valid && callback instanceof Function) { |
@@ -36,2 +36,3 @@ 'use strict'; | ||
this.schema = schema; | ||
this.options = options; | ||
this.path = ctx.path; | ||
@@ -41,2 +42,4 @@ this.propertyPath = ctx.propertyPath; | ||
this.throwError = options && options.throwError; | ||
this.throwFirst = options && options.throwFirst; | ||
this.throwAll = options && options.throwAll; | ||
this.disableFormat = options && options.disableFormat === true; | ||
@@ -56,6 +59,8 @@ }; | ||
if (this.throwError) { | ||
this.errors.push(err); | ||
if (this.throwFirst) { | ||
throw new ValidatorResultError(this); | ||
}else if(this.throwError){ | ||
throw err; | ||
} | ||
this.errors.push(err); | ||
return err; | ||
@@ -83,2 +88,16 @@ }; | ||
module.exports.ValidatorResultError = ValidatorResultError; | ||
function ValidatorResultError(result) { | ||
if(Error.captureStackTrace){ | ||
Error.captureStackTrace(this, ValidatorResultError); | ||
} | ||
this.instance = result.instance; | ||
this.schema = result.schema; | ||
this.options = result.options; | ||
this.errors = result.errors; | ||
} | ||
ValidatorResultError.prototype = new Error(); | ||
ValidatorResultError.prototype.constructor = ValidatorResultError; | ||
ValidatorResultError.prototype.name = "Validation Error"; | ||
/** | ||
@@ -85,0 +104,0 @@ * Describes a problem with a Schema which prevents validation of an instance |
@@ -107,2 +107,4 @@ /* | ||
throwError?: boolean; | ||
throwFirst?: boolean; | ||
throwAll?: boolean; | ||
nestedErrors?: boolean; | ||
@@ -109,0 +111,0 @@ } |
@@ -6,2 +6,3 @@ 'use strict'; | ||
module.exports.ValidatorResult = require('./helpers').ValidatorResult; | ||
module.exports.ValidatorResultError = require('./helpers').ValidatorResultError; | ||
module.exports.ValidationError = require('./helpers').ValidationError; | ||
@@ -8,0 +9,0 @@ module.exports.SchemaError = require('./helpers').SchemaError; |
@@ -9,2 +9,3 @@ 'use strict'; | ||
var ValidatorResult = helpers.ValidatorResult; | ||
var ValidatorResultError = helpers.ValidatorResultError; | ||
var SchemaError = helpers.SchemaError; | ||
@@ -53,3 +54,3 @@ var SchemaContext = helpers.SchemaContext; | ||
var scan = scanSchema(base||anonymousBase, schema); | ||
var ourUri = base || schema.id; | ||
var ourUri = base || schema.$id || schema.id; | ||
for(var uri in scan.id){ | ||
@@ -59,4 +60,6 @@ this.schemas[uri] = scan.id[uri]; | ||
for(var uri in scan.ref){ | ||
// If this schema is already defined, it will be filtered out by the next step | ||
this.unresolvedRefs.push(uri); | ||
} | ||
// Remove newly defined schemas from unresolvedRefs | ||
this.unresolvedRefs = this.unresolvedRefs.filter(function(uri){ | ||
@@ -115,2 +118,3 @@ return typeof self.schemas[uri]==='undefined'; | ||
} | ||
// This section indexes subschemas in the provided schema, so they don't need to be added with Validator#addSchema | ||
// This will work so long as the function at uri.resolve() will resolve a relative URI to a relative URI | ||
@@ -130,5 +134,12 @@ var id = schema.$id || schema.id; | ||
} | ||
if(options.required && instance===undefined){ | ||
var result = new ValidatorResult(instance, schema, options, ctx); | ||
result.addError('is required, but is undefined'); | ||
return result; | ||
} | ||
var result = this.validateSchema(instance, schema, options, ctx); | ||
if (!result) { | ||
throw new Error('Result undefined'); | ||
}else if(options.throwAll && result.errors.length){ | ||
throw new ValidatorResultError(result); | ||
} | ||
@@ -135,0 +146,0 @@ return result; |
{ | ||
"author": "Tom de Grunt <tom@degrunt.nl>", | ||
"name": "jsonschema", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "dependencies": {}, |
@@ -110,2 +110,18 @@ [![Build Status](https://secure.travis-ci.org/tdegrunt/jsonschema.svg)](http://travis-ci.org/tdegrunt/jsonschema) | ||
### Handling `undefined` | ||
`undefined` is not a value known to JSON, and by default, the validator treats it as if it is not invalid. i.e., it will return valid. | ||
```javascript | ||
var res = validate(undefined, {type: 'string'}); | ||
res.valid // true | ||
``` | ||
This behavior may be changed with the "required" option: | ||
```javascript | ||
var res = validate(undefined, {type: 'string'}, {required: true}); | ||
res.valid // false | ||
``` | ||
### Formats | ||
@@ -148,4 +164,11 @@ | ||
The first error found will be thrown as an `Error` object if `options.throwError` is `true`. Otherwise all results will be appended to the `result.errors` array. Each item in this array is a `ValidationError` with the following properties: | ||
By default, results will be returned in a `ValidatorResult` object with the following properties: | ||
* `instance`: any. | ||
* `schema`: Schema. | ||
* `errors`: ValidationError[]. | ||
* `valid`: boolean. | ||
Each item in `errors` is a `ValidationError` with the following properties: | ||
* path: array. An array of property keys or array offsets, indicating where inside objects or arrays the instance was found. | ||
@@ -159,2 +182,12 @@ * property: string. Describes the property path. Starts with `instance`, and is delimited with a dot (`.`). | ||
The validator can be configured to throw in the event of a validation error: | ||
* If the `throwFirst` option is set, the validator will terminate validation at the first encountered error and throw a `ValidatorResultError` object. | ||
* If the `throwAll` option is set, the validator will throw a `ValidatorResultError` object after the entire instance has been validated. | ||
* If the `throwError` option is set, it will throw at the first encountered validation error (like `throwFirst`), but the `ValidationError` object itself will be thrown. Note that, despite the name, this does not inherit from Error like `ValidatorResultError` does. | ||
The `ValidatorResultError` object has the same properties as `ValidatorResult` and additionally inherits from Error. | ||
#### "nestedErrors" option | ||
@@ -161,0 +194,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
77871
1743
423