fix: support required with $data/many properties with ownProperties option, #197

master
Evgeny Poberezkin 2017-02-12 17:58:18 +00:00
parent 176ed87c33
commit 16bf5946fe
2 changed files with 51 additions and 3 deletions

View File

@ -22,6 +22,11 @@
#}}
{{## def.isRequiredOwnProperty:
Object.prototype.hasOwnProperty.call({{=$data}}, {{=$vSchema}}[{{=$i}}])
#}}
{{? !$isData }}
{{? $schema.length < it.opts.loopRequired &&
it.schema.properties && Object.keys(it.schema.properties).length }}
@ -54,7 +59,10 @@
{{?$isData}}{{# def.check$dataIsArray }}{{?}}
for (var {{=$i}} = 0; {{=$i}} < {{=$vSchema}}.length; {{=$i}}++) {
{{=$valid}} = {{=$data}}[{{=$vSchema}}[{{=$i}}]] !== undefined;
{{=$valid}} = {{=$data}}[{{=$vSchema}}[{{=$i}}]] !== undefined
{{? $ownProperties }}
&& {{# def.isRequiredOwnProperty }}
{{?}};
if (!{{=$valid}}) break;
}
@ -77,7 +85,10 @@
{{?}}
for (var {{=$i}} = 0; {{=$i}} < {{=$vSchema}}.length; {{=$i}}++) {
if ({{=$data}}[{{=$vSchema}}[{{=$i}}]] === undefined) {
if ({{=$data}}[{{=$vSchema}}[{{=$i}}]] === undefined
{{? $ownProperties }}
|| !{{# def.isRequiredOwnProperty }}
{{?}}) {
{{# def.addError:'required' }}
}
}

View File

@ -127,6 +127,43 @@ describe('Ajv Options', function () {
test(schema, obj, proto, 1, true);
});
it('should only validate own properties with required keyword - many properties', function() {
ajv = new Ajv({ allErrors: true, loopRequired: 1 });
ajvOP = new Ajv({ ownProperties: true, allErrors: true, loopRequired: 1 });
ajvOP1 = new Ajv({ ownProperties: true, loopRequired: 1 });
var schema = {
required: ['a', 'b', 'c', 'd']
};
var obj = { a: 1, b: 2 };
var proto = { c: 3, d: 4 };
test(schema, obj, proto, 2, true);
});
it('should only validate own properties with required keyword as $data', function() {
ajv = new Ajv({ allErrors: true, $data: true });
ajvOP = new Ajv({ ownProperties: true, allErrors: true, $data: true });
ajvOP1 = new Ajv({ ownProperties: true, $data: true });
var schema = {
required: { $data: '0/req' },
properties: {
req: {
type: 'array',
items: { type: 'string' }
}
}
};
var obj = {
req: ['a', 'b'],
a: 1
};
var proto = { b: 2 };
test(schema, obj, proto, 1, true);
});
it('should only validate own properties with properties and required keyword', function() {
var schema = {
properties: {
@ -225,7 +262,7 @@ describe('Ajv Options', function () {
validateOP(data) .should.equal(false);
validateOP.errors .should.have.length(errors);
validateOP1(data) .should.equal(false);
validateOP1.errors .should.have.length(errors);
validateOP1.errors .should.have.length(1);
} else {
validate(data) .should.equal(false);
validate.errors .should.have.length(errors);