ajv/lib/dot/properties.jst

167 lines
4.8 KiB
Plaintext

{{# def.definitions }}
{{# def.setup:'properties' }}
{{# def.setupNextLevel }}
{{
var $dataNxt = $it.dataLevel = it.dataLevel + 1
, $nextData = 'data' + $dataNxt;
var $pProperties = it.schema.patternProperties || {}
, $pPropertyKeys = Object.keys($pProperties)
, $aProperties = it.schema.additionalProperties
, $noAdditional = $aProperties === false
, $additionalIsSchema = typeof $aProperties == 'object'
&& Object.keys($aProperties).length
, $removeAdditional = it.opts.removeAdditional
, $checkAdditional = $noAdditional || $additionalIsSchema || $removeAdditional;
}}
var {{=$errs}} = errors;
var valid{{=$it.level}} = true;
{{? $checkAdditional }}
var propertiesSchema{{=$lvl}} = validate.schema{{=$schemaPath}} || {};
for (var key{{=$lvl}} in {{=$data}}) {
var isAdditional{{=$lvl}} = propertiesSchema{{=$lvl}}[key{{=$lvl}}] === undefined;
{{? $pPropertyKeys.length }}
if (isAdditional{{=$lvl}}) {
{{~ $pPropertyKeys:$pProperty:$i }}
if ({{= it.usePattern($pProperty) }}.test(key{{=$lvl}}))
isAdditional{{=$lvl}} = false;
{{? $i < $pPropertyKeys.length-1 }}
else
{{?}}
{{~}}
}
{{?}}
if (isAdditional{{=$lvl}}) {
{{? $removeAdditional == 'all' }}
delete {{=$data}}[key{{=$lvl}}];
{{??}}
{{
var $currentErrorPath = it.errorPath;
it.errorPath = it.util.getPathExpr(it.errorPath, 'key' + $lvl, it.opts.jsonPointers);
}}
{{? $noAdditional }}
{{? $removeAdditional }}
delete {{=$data}}[key{{=$lvl}}];
{{??}}
valid{{=$it.level}} = false;
{{# def.error:'additionalProperties' }}
{{? $breakOnError }} break; {{?}}
{{?}}
{{?? $additionalIsSchema }}
{{? $removeAdditional == 'failing' }}
var {{=$errs}} = errors;
{{?}}
{{ /* additionalProperties is schema */
$it.schema = $aProperties;
$it.schemaPath = it.schemaPath + '.additionalProperties';
$it.errorPath = it.errorPath;
var $passData = $data + '[key' + $lvl + ']';
}}
{{ var $code = it.validate($it); }}
{{# def.optimizeValidate }}
{{? $removeAdditional == 'failing' }}
if (!valid{{=$it.level}}) {
errors = {{=$errs}};
if (validate.errors !== null) {
if (errors) validate.errors.length = errors;
else validate.errors = null;
}
delete {{=$data}}[key{{=$lvl}}];
}
{{??}}
{{? $breakOnError }} if (!valid{{=$it.level}}) break; {{?}}
{{?}}
{{?}}
{{ it.errorPath = $currentErrorPath; }}
{{?}}
}
}
{{# def.ifResultValid }}
{{?}}
{{? $schema }}
{{ for (var $propertyKey in $schema) { }}
{{ var $sch = $schema[$propertyKey]; }}
{{? {{# def.nonEmptySchema:$sch}} }}
{{
$it.schema = $sch;
var $prop = it.util.getProperty($propertyKey)
, $passData = $data + $prop;
$it.schemaPath = $schemaPath + $prop;
$it.errorPath = it.util.getPath(it.errorPath, $propertyKey, it.opts.jsonPointers);
}}
{{ var $code = it.validate($it); }}
{{? {{# def.willOptimize }} }}
{{
$code = {{# def._optimizeValidate }};
var $useData = $passData;
}}
{{??}}
{{ var $useData = $nextData; }}
var {{=$nextData}} = {{=$passData}};
{{?}}
{{? $breakOnError }}
if ({{=$useData}} === undefined) {
valid{{=$it.level}} = true;
} else {
{{??}}
if ({{=$useData}} !== undefined) {
{{?}}
{{= $code }}
}
{{?}} {{ /* def.nonEmptySchema */ }}
{{# def.ifResultValid }}
{{ } }}
{{?}}
{{~ $pPropertyKeys:$pProperty }}
{{ var $sch = $pProperties[$pProperty]; }}
{{? {{# def.nonEmptySchema:$sch}} }}
{{
$it.schema = $sch;
$it.schemaPath = it.schemaPath + '.patternProperties' + it.util.getProperty($pProperty);
}}
for (var key{{=$lvl}} in {{=$data}}) {
if ({{= it.usePattern($pProperty) }}.test(key{{=$lvl}})) {
{{
$it.errorPath = it.util.getPathExpr(it.errorPath, 'key' + $lvl, it.opts.jsonPointers);
var $passData = $data + '[key' + $lvl + ']';
}}
{{ var $code = it.validate($it); }}
{{# def.optimizeValidate }}
{{? $breakOnError }} if (!valid{{=$it.level}}) break; {{?}}
}
{{? $breakOnError }} else valid{{=$it.level}} = true; {{?}}
}
{{# def.ifResultValid }}
{{?}} {{ /* def.nonEmptySchema */ }}
{{~}}
{{? $breakOnError }}
{{= $closingBraces }}
if ({{=$errs}} == errors) {
{{?}}
{{# def.cleanUp }}