From 1babc9df49d94c0b422cd3c1f2305d46b9411955 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sun, 12 Feb 2017 00:02:56 +0000 Subject: [PATCH] fix: support property dependecies with ownProperties option, #197 --- lib/dot/dependencies.jst | 3 +++ spec/options.spec.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/dot/dependencies.jst b/lib/dot/dependencies.jst index 2e42ee5..06d0a3e 100644 --- a/lib/dot/dependencies.jst +++ b/lib/dot/dependencies.jst @@ -26,6 +26,9 @@ var missing{{=$lvl}}; {{ $deps = $propertyDeps[$property]; }} {{? $deps.length }} if ({{=$data}}{{= it.util.getProperty($property) }} !== undefined + {{? $ownProperties }} + && Object.prototype.hasOwnProperty.call({{=$data}}, '{{=it.util.escapeQuotes($property)}}') + {{?}} {{? $breakOnError }} && ({{# def.checkMissingProperty:$deps }})) { {{# def.errorMissingProperty:'dependencies' }} diff --git a/spec/options.spec.js b/spec/options.spec.js index fbc096f..a1bef53 100644 --- a/spec/options.spec.js +++ b/spec/options.spec.js @@ -141,6 +141,23 @@ describe('Ajv Options', function () { test(schema, obj, proto, 1, true); }); + it('should only validate own properties with dependencies keyword', function() { + var schema = { + dependencies: { + a: ['c'], + b: ['d'] + } + }; + + var obj = { a: 1, c: 3 }; + var proto = { b: 2 }; + test(schema, obj, proto); + + obj = { a: 1, b: 2, c: 3 }; + proto = { d: 4 }; + test(schema, obj, proto, 1, true); + }); + it('should only validate own properties with patternProperties', function() { var schema = { patternProperties: { 'f.*o': { type: 'integer' } },