fix: support schema dependencies with ownProperties option, #197
parent
647cd6eccb
commit
176ed87c33
|
@ -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);
|
||||
|
|
|
@ -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' } },
|
||||
|
|
Loading…
Reference in New Issue