fix: support schema dependencies with ownProperties option, #197

master
Evgeny Poberezkin 2017-02-12 00:36:14 +00:00
parent 647cd6eccb
commit 176ed87c33
2 changed files with 27 additions and 5 deletions

View File

@ -5,6 +5,14 @@
{{# def.setupNextLevel }}
{{## def.propertyInData:
{{=$data}}{{= it.util.getProperty($property) }} !== undefined
{{? $ownProperties }}
&& Object.prototype.hasOwnProperty.call({{=$data}}, '{{=it.util.escapeQuotes($property)}}')
{{?}}
#}}
{{
var $schemaDeps = {}
, $propertyDeps = {}
@ -25,10 +33,7 @@ var missing{{=$lvl}};
{{ for (var $property in $propertyDeps) { }}
{{ $deps = $propertyDeps[$property]; }}
{{? $deps.length }}
if ({{=$data}}{{= it.util.getProperty($property) }} !== undefined
{{? $ownProperties }}
&& Object.prototype.hasOwnProperty.call({{=$data}}, '{{=it.util.escapeQuotes($property)}}')
{{?}}
if ({{# def.propertyInData }}
{{? $breakOnError }}
&& ({{# def.checkMissingProperty:$deps }})) {
{{# def.errorMissingProperty:'dependencies' }}
@ -53,7 +58,7 @@ var missing{{=$lvl}};
{{? {{# def.nonEmptySchema:$sch }} }}
{{=$nextValid}} = true;
if ({{=$data}}{{= it.util.getProperty($property) }} !== undefined) {
if ({{# def.propertyInData }}) {
{{
$it.schema = $sch;
$it.schemaPath = $schemaPath + it.util.getProperty($property);

View File

@ -158,6 +158,23 @@ describe('Ajv Options', function () {
test(schema, obj, proto, 1, true);
});
it('should only validate own properties with schema dependencies', function() {
var schema = {
dependencies: {
a: { not: { required: ['c'] } },
b: { not: { required: ['d'] } }
}
};
var obj = { a: 1, d: 3 };
var proto = { b: 2 };
test(schema, obj, proto);
obj = { a: 1, b: 2 };
proto = { d: 4 };
test(schema, obj, proto);
});
it('should only validate own properties with patternProperties', function() {
var schema = {
patternProperties: { 'f.*o': { type: 'integer' } },