Skip to content

Commit

Permalink
"parentSchema" property added to error objects when using "verbose" o…
Browse files Browse the repository at this point in the history
…ption
  • Loading branch information
epoberezkin committed Dec 11, 2015
1 parent 610dcd1 commit 7c7abbb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ Defaults:

- _allErrors_: check all rules collecting all errors. Default is to return after the first error.
- _removeAdditional_: remove additional properties. Default is not to remove. If the option is 'all', then all additional properties are removed, regardless of `additionalProperties` keyword in schema (and no validation is made for them). If the option is `true` (or truthy), only additional properties with `additionalProperties` keyword equal to `false` are removed. If the option is 'failing', then additional properties that fail schema validation will be removed too (where `additionalProperties` keyword is schema).
- _verbose_: include the reference to the part of the schema and validated data in errors (false by default).
- _verbose_: include the reference to the part of the schema (`schema` and `parentSchema`) and validated data in errors (false by default).
- _format_: formats validation mode ('fast' by default). Pass 'full' for more correct and slow validation or `false` not to validate formats at all. E.g., 25:00:00 and 2015/14/33 will be invalid time and date in 'full' mode but it will be valid in 'fast' mode.
- _formats_: an object with custom formats. Keys and values will be passed to `addFormat` method.
- _schemas_: an array or object of schemas that will be added to the instance. If the order is important, pass array. In this case schemas must have IDs in them. Otherwise the object can be passed - `addSchema(value, key)` will be called for each schema in this object.
Expand Down Expand Up @@ -565,6 +565,7 @@ Each error is an object with the following properties:
- _params_: the object with the additional information about error that can be used to create custom error messages (e.g., using [ajv-i18n](https://github.com/epoberezkin/ajv-i18n) package). See below for parameters set by all keywords.
- _message_: the standard error message (can be excluded with option `messages` set to false).
- _schema_: the schema of the keyword (added with `verbose` option).
- _parentSchema_: the schema containing the keyword (added with `verbose` option)
- _data_: the data validated by the keyword (added with `verbose` option).


Expand Down
1 change: 1 addition & 0 deletions lib/dot/definitions.def
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
{{?}}
{{? it.opts.verbose }}
, schema: {{# def._errorSchemas[_rule] }}
, parentSchema: validate.schema{{=it.schemaPath}}
, data: {{=$data}}
{{?}}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "2.2.2",
"version": "2.3.0",
"description": "Another JSON Schema Validator",
"main": "lib/ajv.js",
"files": [
Expand Down
23 changes: 23 additions & 0 deletions spec/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,27 @@ describe('Ajv Options', function () {
}
});
});


describe('verbose', function() {
it('should add schema, parentSchema and data to errors with verbose option == true', function() {
testVerbose(Ajv({ verbose: true }));
testVerbose(Ajv({ verbose: true, allErrors: true }));

function testVerbose(ajv) {
var schema = { properties: { foo: { minimum: 5 } } };
var validate = ajv.compile(schema);

var data = { foo: 3 };
validate(data) .should.equal(false);
validate.errors .should.have.length(1);
var err = validate.errors[0];

should.equal(err.schema, 5);
err.parentSchema .should.eql({ minimum: 5 });
err.parentSchema .should.equal(schema.properties.foo); // by reference
err.data .should.equal(3);
}
});
});
});

0 comments on commit 7c7abbb

Please sign in to comment.