ajv/lib/dot/validate.jst

151 lines
4.1 KiB
Plaintext
Raw Normal View History

{{# def.definitions }}
2015-12-12 16:41:51 +03:00
{{# def.errors }}
{{# def.defaults }}
{{ /**
* 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.resolve.fullPath(it.root.schema.id);
it.baseId = it.baseId || it.rootId;
delete it.isTop;
2015-12-20 23:42:33 +03:00
if (it.opts.v5) it.dataPathArr = [undefined];
2015-06-02 03:59:32 +03:00
}}
validate = function (data, dataPath) {
'use strict';
var vErrors = null; {{ /* don't edit, used in replace */ }}
var errors = 0; {{ /* don't edit, used in replace */ }}
{{??}}
{{
var $lvl = it.level
, $dataLvl = it.dataLevel
, $data = 'data' + ($dataLvl || '');
if (it.schema.id) it.baseId = it.resolve.url(it.baseId, it.schema.id);
}}
var errs_{{=$lvl}} = errors;
{{?}}
{{
var $valid = 'valid' + $lvl
, $breakOnError = !it.opts.allErrors
, $closingBraces1 = ''
, $closingBraces2 = ''
, $errorKeyword;
var $typeSchema = it.schema.type;
}}
{{~ it.RULES:$rulesGroup }}
{{? $shouldUseGroup($rulesGroup) }}
{{? $rulesGroup.type }}
if ({{= it.util.checkDataType($rulesGroup.type, $data) }}) {
{{?}}
{{? it.opts.useDefaults && !it.compositeRule }}
{{? $rulesGroup.type == 'object' && it.schema.properties }}
{{# def.defaultProperties }}
{{?? $rulesGroup.type == 'array' && Array.isArray(it.schema.items) }}
{{# def.defaultItems }}
{{?}}
{{?}}
{{~ $rulesGroup.rules:$rule }}
{{? $shouldUseRule($rule) }}
{{? $rule.custom }}
{{# def.custom }}
{{??}}
2015-12-22 20:03:09 +03:00
{{= $rule.code(it, $rule.keyword) }}
{{?}}
{{? $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 {
2015-12-19 13:52:39 +03:00
{{
var $schemaPath = it.schemaPath + '.type'
, $errSchemaPath = it.errSchemaPath + '/type';
}}
{{# def.error:'type' }}
}
{{?}}
{{?}}
{{? $breakOnError }}
if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
{{ $closingBraces2 += '}'; }}
{{?}}
{{?}}
{{~}}
{{? $typeSchema && !$typeChecked }}
{{
var $schemaPath = it.schemaPath + '.type'
2015-12-19 13:52:39 +03:00
, $errSchemaPath = it.errSchemaPath + '/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 }}
validate.errors = vErrors; {{ /* don't edit, used in replace */ }}
return errors === 0; {{ /* don't edit, used in replace */ }}
}
{{??}}
var {{=$valid}} = errors === errs_{{=$lvl}};
{{?}}
{{# def.cleanUp }}
{{? $top && $breakOnError }}
{{# def.cleanUpVarErrors }}
{{?}}
{{
function $shouldUseGroup($rulesGroup) {
for (var i=0; i < $rulesGroup.rules.length; i++)
if ($shouldUseRule($rulesGroup.rules[i]))
return true;
}
function $shouldUseRule($rule) {
return it.schema[$rule.keyword] !== undefined ||
( $rule.keyword == 'properties' &&
( it.schema.additionalProperties === false ||
2015-11-28 17:59:08 +03:00
typeof it.schema.additionalProperties == 'object'
|| ( it.schema.patternProperties &&
Object.keys(it.schema.patternProperties).length )
|| ( it.opts.v5 && it.schema.patternGroups &&
Object.keys(it.schema.patternGroups).length )));
}
2015-05-31 14:21:41 +03:00
}}