ajv/lib/dot/validate.jst

129 lines
3.2 KiB
Plaintext
Raw Normal View History

{{# def.definitions }}
{{ /**
* schema compilation (render) time:
* it = { schema, RULES, _validate, opts }
* it.validate - this template function,
* it is used recursively to generate code for subschemas
*
* runtime:
* "validate" is a variable name to which this function will be assigned
2015-05-31 14:21:41 +03:00
* validateRef etc. are defined in the parent scope in index.js
*/ }}
{{? it.isTop}}
2015-06-02 03:59:32 +03:00
{{
var $top = it.isTop
, $lvl = it.level = 0
, $dataLvl = it.dataLevel = 0
, $data = 'data';
it.rootId = it.baseId = it.resolve.fullPath(it.root.schema.id);
delete it.isTop;
it.wasTop = true;
2015-06-02 03:59:32 +03:00
}}
validate = function (data, dataPath) {
'use strict';
validate.errors = null;{{ /* don't edit, used in replace */ }}
var errors = 0; {{ /* don't edit, used in replace */ }}
{{??}}
{{? it.opts._debug }} console.log('validate dataPath:', dataPath); {{?}}
{{
var $lvl = it.level
, $dataLvl = it.dataLevel
, $data = 'data' + ($dataLvl || '');
if (it.schema.id) it.baseId = it.resolve.url(it.baseId, it.schema.id);
delete it.wasTop;
}}
var errs_{{=$lvl}} = errors;
{{?}}
{{
var $valid = 'valid' + $lvl
, $breakOnError = !it.opts.allErrors
, $closingBraces1 = ''
, $closingBraces2 = '';
var $typeSchema = it.schema.type;
}}
{{~ it.RULES:$rulesGroup }}
{{? $shouldUseGroup($rulesGroup) }}
{{? $rulesGroup.type }}
if ({{= it.util.checkDataType($rulesGroup.type, $data) }}) {
{{?}}
{{~ $rulesGroup.rules:$rule }}
{{? $shouldUseRule($rule) }}
{{= $rule.code(it) }}
{{? $breakOnError }}
{{ $closingBraces1 += '}'; }}
{{?}}
2015-06-02 03:59:32 +03:00
{{?}}
{{~}}
{{? $breakOnError }}
{{= $closingBraces1 }}
{{ $closingBraces1 = ''; }}
2015-06-02 03:59:32 +03:00
{{?}}
{{? $rulesGroup.type }}
}
{{? $typeSchema && $typeSchema === $rulesGroup.type }}
{{ var $typeChecked = true; }}
else {
{{# def.error:'type' }}
}
{{?}}
{{?}}
{{? $breakOnError }}
if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
{{ $closingBraces2 += '}'; }}
{{?}}
{{?}}
{{~}}
{{? $typeSchema && !$typeChecked }}
{{
var $schemaPath = it.schemaPath + '.type'
, $isArray = Array.isArray($typeSchema)
, $method = $isArray ? 'checkDataTypes' : 'checkDataType';
}}
if ({{= it.util[$method]($typeSchema, $data, true) }}) {
{{# def.error:'type' }}
}
{{?}}
{{? $breakOnError }} {{= $closingBraces2 }} {{?}}
2015-06-02 03:59:32 +03:00
{{? $top }}
return errors === 0;{{ /* don't edit, used in replace */ }}
}
{{??}}
var {{=$valid}} = errors === errs_{{=$lvl}};
{{?}}
{{# def.cleanUp }}
{{? $top && $breakOnError }}
{{# def.cleanUpVarErrors }}
{{?}}
{{
function $shouldUseGroup($rulesGroup) {
return $rulesGroup.rules.some(function ($rule) {
return $shouldUseRule($rule);
});
}
function $shouldUseRule($rule) {
return it.schema[$rule.keyword] !== undefined ||
( $rule.keyword == 'properties' &&
( it.schema.additionalProperties !== undefined ||
( it.schema.patternProperties &&
Object.keys(it.schema.patternProperties).length )));
}
2015-05-31 14:21:41 +03:00
}}