diff --git a/lib/dot/dependencies.jst b/lib/dot/dependencies.jst index c5b8f89..c41f334 100644 --- a/lib/dot/dependencies.jst +++ b/lib/dot/dependencies.jst @@ -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); diff --git a/spec/options.spec.js b/spec/options.spec.js index a1bef53..0c5494c 100644 --- a/spec/options.spec.js +++ b/spec/options.spec.js @@ -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' } },