fix: ownProperties option with required keyword, #197

master
Evgeny Poberezkin 2017-02-11 23:44:59 +00:00
parent 57fc360285
commit df6ef691ae
6 changed files with 32 additions and 25 deletions

View File

@ -180,3 +180,16 @@
if (!Object.prototype.hasOwnProperty.call({{=$data}}, {{=$key}})) continue;
{{?}}
#}}
{{## def.noPropertyInData:
{{=$useData}} === undefined
{{? $ownProperties }}
|| !{{# def.isOwnProperty }}
{{?}}
#}}
{{## def.isOwnProperty:
Object.prototype.hasOwnProperty.call({{=$data}}, '{{=it.util.escapeQuotes($propertyKey)}}')
#}}

View File

@ -7,7 +7,8 @@
{{
var $schemaDeps = {}
, $propertyDeps = {};
, $propertyDeps = {}
, $ownProperties = it.opts.ownProperties;
for ($property in $schema) {
var $sch = $schema[$property];
@ -30,7 +31,7 @@ var missing{{=$lvl}};
{{# def.errorMissingProperty:'dependencies' }}
{{??}}
) {
{{~ $deps:$reqProperty }}
{{~ $deps:$propertyKey }}
{{# def.allErrorsMissingProperty:'dependencies' }}
{{~}}
{{?}}

View File

@ -1,8 +1,11 @@
{{## def.checkMissingProperty:_properties:
{{~ _properties:_$property:$i }}
{{~ _properties:$propertyKey:$i }}
{{?$i}} || {{?}}
{{ var $prop = it.util.getProperty(_$property); }}
( {{=$data}}{{=$prop}} === undefined && (missing{{=$lvl}} = {{= it.util.toQuotedString(it.opts.jsonPointers ? _$property : $prop) }}) )
{{
var $prop = it.util.getProperty($propertyKey)
, $useData = $data + $prop;
}}
( ({{# def.noPropertyInData }}) && (missing{{=$lvl}} = {{= it.util.toQuotedString(it.opts.jsonPointers ? $propertyKey : $prop) }}) )
{{~}}
#}}
@ -20,15 +23,17 @@
{{# def.error:_error }}
#}}
{{## def.allErrorsMissingProperty:_error:
{{
var $prop = it.util.getProperty($reqProperty)
, $missingProperty = it.util.escapeQuotes($reqProperty);
var $prop = it.util.getProperty($propertyKey)
, $missingProperty = it.util.escapeQuotes($propertyKey)
, $useData = $data + $prop;
if (it.opts._errorDataPathProperty) {
it.errorPath = it.util.getPath($currentErrorPath, $reqProperty, it.opts.jsonPointers);
it.errorPath = it.util.getPath($currentErrorPath, $propertyKey, it.opts.jsonPointers);
}
}}
if ({{=$data}}{{=$prop}} === undefined) {
if ({{# def.noPropertyInData }}) {
{{# def.addError:_error }}
}
#}}

View File

@ -21,19 +21,6 @@
#}}
{{## def.noPropertyInData:
{{=$useData}} === undefined
{{? $ownProperties }}
|| !{{# def.isOwnProperty }}
{{?}}
#}}
{{## def.isOwnProperty:
Object.prototype.hasOwnProperty.call({{=$data}}, '{{=it.util.escapeQuotes($propertyKey)}}')
#}}
{{
var $key = 'key' + $lvl
, $dataNxt = $it.dataLevel = it.dataLevel + 1

View File

@ -41,7 +41,8 @@
{{? $isData || $required.length }}
{{
var $currentErrorPath = it.errorPath
, $loopRequired = $isData || $required.length >= it.opts.loopRequired;
, $loopRequired = $isData || $required.length >= it.opts.loopRequired
, $ownProperties = it.opts.ownProperties;
}}
{{? $breakOnError }}
@ -83,7 +84,7 @@
{{? $isData }} } {{?}}
{{??}}
{{~ $required:$reqProperty }}
{{~ $required:$propertyKey }}
{{# def.allErrorsMissingProperty:'required' }}
{{~}}
{{?}}

View File

@ -117,7 +117,7 @@ describe('Ajv Options', function () {
test(schema, obj, proto);
});
it.skip('should only validate own properties with required keyword', function() {
it('should only validate own properties with required keyword', function() {
var schema = {
required: ['a', 'b']
};