Skip to content

Commit

Permalink
optimised "not" keyword in {allErrors: true} mode to fail on the firs…
Browse files Browse the repository at this point in the history
…t error in subschema, closes #131
  • Loading branch information
epoberezkin committed Feb 27, 2016
1 parent ec0f1c1 commit f48efb5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/dot/not.jst
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,19 @@

{{# def.setCompositeRule }}

{{ $it.createErrors = false; }}
{{
$it.createErrors = false;
var $allErrorsOption;
if ($it.opts.allErrors) {
$allErrorsOption = $it.opts.allErrors;
$it.opts.allErrors = false;
}
}}
{{= it.validate($it) }}
{{ $it.createErrors = true; }}
{{
$it.createErrors = true;
if ($allErrorsOption) $it.opts.allErrors = $allErrorsOption;
}}

{{# def.resetCompositeRule }}

Expand Down
45 changes: 45 additions & 0 deletions spec/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,4 +547,49 @@ describe('Ajv Options', function () {
return storeContext;
}
});


describe('allErrors', function() {
it('should be disabled inside "not" keyword', function() {
test(Ajv(), false);
test(Ajv({ allErrors: true }), true);

function test(ajv, allErrors) {
var format1called = false
, format2called = false;

ajv.addFormat('format1', function() {
format1called = true;
return false;
});

ajv.addFormat('format2', function() {
format2called = true;
return false;
});

var schema1 = {
allOf: [
{ format: 'format1' },
{ format: 'format2' }
]
};

ajv.validate(schema1, 'abc') .should.equal(false);
ajv.errors .should.have.length(allErrors ? 2 : 1);
format1called .should.equal(true);
format2called .should.equal(allErrors);

var schema2 = {
not: schema1
};

format1called = format2called = false;
ajv.validate(schema2, 'abc') .should.equal(true);
should.equal(ajv.errors, null);
format1called .should.equal(true);
format2called .should.equal(false);
}
});
});
});

0 comments on commit f48efb5

Please sign in to comment.