Skip to content

Commit

Permalink
dataPath using jsonPointers in "required" keyword when allErrors == f…
Browse files Browse the repository at this point in the history
…alse, #32
  • Loading branch information
epoberezkin committed Aug 22, 2015
1 parent 4c4fdd5 commit f9017a5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
10 changes: 6 additions & 4 deletions lib/dot/required.jst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{{~ $schema:$property:$i }}
{{? $i}} || {{?}}
{{ var $prop = it.util.getProperty($property); }}
( {{=$data}}{{=$prop}} === undefined && (missing{{=$lvl}} = {{= it.util.toQuotedString($prop) }}) )
( {{=$data}}{{=$prop}} === undefined && (missing{{=$lvl}} = {{= it.util.toQuotedString(it.opts.jsonPointers ? $property : $prop) }}) )
{{~}}
#}}

Expand All @@ -27,9 +27,11 @@
{{? $schema.length <= 20 }}
if ({{# def.checkRequired }}) {
{{
var $propertyPath = ' + missing' + $lvl
, $missingProperty = '\'' + $propertyPath + ' + \'';
it.errorPath = $currentErrorPath + $propertyPath;
var $propertyPath = 'missing' + $lvl
, $missingProperty = '\' + ' + $propertyPath + ' + \'';
it.errorPath = it.opts.jsonPointers
? it.util.getPathExpr($currentErrorPath, $propertyPath, true)
: $currentErrorPath + ' + ' + $propertyPath;
}}
{{# def.error:'required' }}
} else {
Expand Down
8 changes: 4 additions & 4 deletions lib/dotjs/required.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ module.exports = function anonymous(it) {
out += ' || ';
}
var $prop = it.util.getProperty($property);
out += ' ( ' + ($data) + ($prop) + ' === undefined && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString($prop)) + ') ) ';
out += ' ( ' + ($data) + ($prop) + ' === undefined && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString(it.opts.jsonPointers ? $property : $prop)) + ') ) ';
}
}
out += ') { ';
var $propertyPath = ' + missing' + $lvl,
$missingProperty = '\'' + $propertyPath + ' + \'';
it.errorPath = $currentErrorPath + $propertyPath;
var $propertyPath = 'missing' + $lvl,
$missingProperty = '\' + ' + $propertyPath + ' + \'';
it.errorPath = it.opts.jsonPointers ? it.util.getPathExpr($currentErrorPath, $propertyPath, true) : $currentErrorPath + ' + ' + $propertyPath;
if (it.wasTop && $breakOnError) {
out += ' validate.errors = [ { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' ';
if (it.opts.verbose) {
Expand Down
25 changes: 23 additions & 2 deletions spec/errors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ var Ajv = require(typeof window == 'object' ? 'ajv' : '../lib/ajv')


describe('Validation errors', function () {
var ajv, fullAjv;
var ajv, ajvJP, fullAjv;

beforeEach(function() {
ajv = Ajv();
fullAjv = Ajv({ allErrors: true, beautify: true, jsonPointers: true });
ajvJP = Ajv({ jsonPointers: true });
fullAjv = Ajv({ allErrors: true, jsonPointers: true });
});

it('error should include dataPath', function() {
Expand Down Expand Up @@ -50,6 +51,11 @@ describe('Validation errors', function () {
shouldBeInvalid(validate, invalidData);
shouldBeError(validate.errors[0], 'additionalProperties', "['baz']");

var validateJP = ajvJP.compile(schema);
shouldBeValid(validateJP, data);
shouldBeInvalid(validateJP, invalidData);
shouldBeError(validateJP.errors[0], 'additionalProperties', "/baz");

var fullValidate = fullAjv.compile(schema);
shouldBeValid(fullValidate, data);
shouldBeInvalid(fullValidate, invalidData, 2);
Expand Down Expand Up @@ -81,6 +87,13 @@ describe('Validation errors', function () {
shouldBeInvalid(validate, invalidData2);
shouldBeError(validate.errors[0], 'required', '.foo', 'property .foo is required');

var validateJP = ajvJP.compile(schema);
shouldBeValid(validateJP, data);
shouldBeInvalid(validateJP, invalidData1);
shouldBeError(validateJP.errors[0], 'required', '/bar', 'property bar is required');
shouldBeInvalid(validateJP, invalidData2);
shouldBeError(validateJP.errors[0], 'required', '/foo', 'property foo is required');

var fullValidate = fullAjv.compile(schema);
shouldBeValid(fullValidate, data);
shouldBeInvalid(fullValidate, invalidData1);
Expand Down Expand Up @@ -112,6 +125,13 @@ describe('Validation errors', function () {
shouldBeInvalid(validate, invalidData2);
shouldBeError(validate.errors[0], 'required', "['2']", "property '2' is required");

var validateJP = ajvJP.compile(schema);
shouldBeValid(validateJP, data);
shouldBeInvalid(validateJP, invalidData1);
shouldBeError(validateJP.errors[0], 'required', "/1", "property '1' is required");
shouldBeInvalid(validateJP, invalidData2);
shouldBeError(validateJP.errors[0], 'required', "/2", "property '2' is required");

var fullValidate = fullAjv.compile(schema);
shouldBeValid(fullValidate, data);
shouldBeInvalid(fullValidate, invalidData1);
Expand All @@ -124,6 +144,7 @@ describe('Validation errors', function () {

function testSchema1(schema) {
_testSchema1(ajv, schema);
_testSchema1(ajvJP, schema);
_testSchema1(fullAjv, schema)
}

Expand Down

0 comments on commit f9017a5

Please sign in to comment.