2015-06-06 16:12:13 +03:00
|
|
|
{{# def.definitions }}
|
2015-12-12 16:41:51 +03:00
|
|
|
{{# def.errors }}
|
2015-06-06 16:12:13 +03:00
|
|
|
|
2015-05-28 03:24:42 +03:00
|
|
|
{{ /**
|
2015-05-30 21:11:06 +03:00
|
|
|
* schema compilation (render) time:
|
2015-05-28 03:24:42 +03:00
|
|
|
* it = { schema, RULES, _validate, opts }
|
2015-05-31 01:04:46 +03:00
|
|
|
* it.validate - this template function,
|
|
|
|
* it is used recursively to generate code for subschemas
|
2015-05-30 21:11:06 +03:00
|
|
|
*
|
|
|
|
* runtime:
|
2015-05-31 01:04:46 +03:00
|
|
|
* "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
|
2015-05-28 03:24:42 +03:00
|
|
|
*/ }}
|
|
|
|
|
2015-06-16 17:10:21 +03:00
|
|
|
{{? it.isTop}}
|
2015-06-02 03:59:32 +03:00
|
|
|
{{
|
2015-06-16 17:10:21 +03:00
|
|
|
var $top = it.isTop
|
2015-06-06 16:12:13 +03:00
|
|
|
, $lvl = it.level = 0
|
|
|
|
, $dataLvl = it.dataLevel = 0
|
|
|
|
, $data = 'data';
|
2015-10-25 02:57:42 +03:00
|
|
|
it.rootId = it.resolve.fullPath(it.root.schema.id);
|
|
|
|
it.baseId = it.baseId || it.rootId;
|
2015-06-16 17:10:21 +03:00
|
|
|
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
|
|
|
}}
|
|
|
|
|
2015-06-13 13:40:45 +03:00
|
|
|
validate = function (data, dataPath) {
|
2015-06-14 01:41:33 +03:00
|
|
|
'use strict';
|
2015-07-04 03:37:35 +03:00
|
|
|
var vErrors = null; {{ /* don't edit, used in replace */ }}
|
|
|
|
var errors = 0; {{ /* don't edit, used in replace */ }}
|
2015-06-03 03:36:59 +03:00
|
|
|
{{??}}
|
2015-07-29 02:34:46 +03:00
|
|
|
{{
|
2015-06-04 02:17:33 +03:00
|
|
|
var $lvl = it.level
|
2015-06-06 16:12:13 +03:00
|
|
|
, $dataLvl = it.dataLevel
|
|
|
|
, $data = 'data' + ($dataLvl || '');
|
2015-06-07 14:46:53 +03:00
|
|
|
|
2015-10-25 02:57:42 +03:00
|
|
|
if (it.schema.id) it.baseId = it.resolve.url(it.baseId, it.schema.id);
|
2015-06-03 03:36:59 +03:00
|
|
|
}}
|
|
|
|
|
2015-06-14 11:04:11 +03:00
|
|
|
var errs_{{=$lvl}} = errors;
|
|
|
|
{{?}}
|
2015-06-13 13:40:45 +03:00
|
|
|
|
2015-06-03 03:36:59 +03:00
|
|
|
{{
|
2015-06-05 02:42:34 +03:00
|
|
|
var $valid = 'valid' + $lvl
|
2015-06-14 01:41:33 +03:00
|
|
|
, $breakOnError = !it.opts.allErrors
|
2015-06-03 03:36:59 +03:00
|
|
|
, $closingBraces1 = ''
|
2015-12-23 04:22:26 +03:00
|
|
|
, $closingBraces2 = ''
|
|
|
|
, $errorKeyword;
|
2015-06-06 16:12:13 +03:00
|
|
|
|
|
|
|
var $typeSchema = it.schema.type;
|
2015-06-03 03:36:59 +03:00
|
|
|
}}
|
2015-05-30 21:11:06 +03:00
|
|
|
|
2015-06-04 23:43:16 +03:00
|
|
|
{{~ it.RULES:$rulesGroup }}
|
|
|
|
{{? $shouldUseGroup($rulesGroup) }}
|
2015-06-06 16:12:13 +03:00
|
|
|
{{? $rulesGroup.type }}
|
|
|
|
if ({{= it.util.checkDataType($rulesGroup.type, $data) }}) {
|
|
|
|
{{?}}
|
2015-06-04 23:43:16 +03:00
|
|
|
{{~ $rulesGroup.rules:$rule }}
|
|
|
|
{{? $shouldUseRule($rule) }}
|
2015-11-11 10:01:27 +03:00
|
|
|
{{? $rule.custom }}
|
2015-11-21 15:43:44 +03:00
|
|
|
{{# def.custom }}
|
2015-11-11 10:01:27 +03:00
|
|
|
{{??}}
|
2015-12-22 20:03:09 +03:00
|
|
|
{{= $rule.code(it, $rule.keyword) }}
|
2015-11-11 10:01:27 +03:00
|
|
|
{{?}}
|
2015-06-14 01:41:33 +03:00
|
|
|
{{? $breakOnError }}
|
2015-06-04 23:43:16 +03:00
|
|
|
{{ $closingBraces1 += '}'; }}
|
2015-05-31 01:04:46 +03:00
|
|
|
{{?}}
|
2015-06-02 03:59:32 +03:00
|
|
|
{{?}}
|
2015-06-04 23:43:16 +03:00
|
|
|
{{~}}
|
2015-06-14 01:41:33 +03:00
|
|
|
{{? $breakOnError }}
|
2015-06-04 23:43:16 +03:00
|
|
|
{{= $closingBraces1 }}
|
|
|
|
{{ $closingBraces1 = ''; }}
|
2015-06-02 03:59:32 +03:00
|
|
|
{{?}}
|
2015-06-06 16:12:13 +03:00
|
|
|
{{? $rulesGroup.type }}
|
|
|
|
}
|
|
|
|
{{? $typeSchema && $typeSchema === $rulesGroup.type }}
|
|
|
|
{{ var $typeChecked = true; }}
|
2015-06-13 15:01:05 +03:00
|
|
|
else {
|
2015-12-19 13:52:39 +03:00
|
|
|
{{
|
|
|
|
var $schemaPath = it.schemaPath + '.type'
|
|
|
|
, $errSchemaPath = it.errSchemaPath + '/type';
|
|
|
|
}}
|
2015-06-06 16:12:13 +03:00
|
|
|
{{# def.error:'type' }}
|
2015-06-13 15:01:05 +03:00
|
|
|
}
|
2015-06-06 16:12:13 +03:00
|
|
|
{{?}}
|
|
|
|
{{?}}
|
2015-06-04 23:43:16 +03:00
|
|
|
|
2015-06-14 01:41:33 +03:00
|
|
|
{{? $breakOnError }}
|
2015-06-16 17:10:21 +03:00
|
|
|
if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
|
2015-06-04 23:43:16 +03:00
|
|
|
{{ $closingBraces2 += '}'; }}
|
2015-05-30 21:11:06 +03:00
|
|
|
{{?}}
|
2015-06-04 23:43:16 +03:00
|
|
|
{{?}}
|
|
|
|
{{~}}
|
2015-05-30 21:11:06 +03:00
|
|
|
|
2015-06-06 16:12:13 +03:00
|
|
|
{{? $typeSchema && !$typeChecked }}
|
|
|
|
{{
|
|
|
|
var $schemaPath = it.schemaPath + '.type'
|
2015-12-19 13:52:39 +03:00
|
|
|
, $errSchemaPath = it.errSchemaPath + '/type'
|
2015-06-06 16:12:13 +03:00
|
|
|
, $isArray = Array.isArray($typeSchema)
|
|
|
|
, $method = $isArray ? 'checkDataTypes' : 'checkDataType';
|
|
|
|
}}
|
|
|
|
|
2015-06-14 11:04:11 +03:00
|
|
|
if ({{= it.util[$method]($typeSchema, $data, true) }}) {
|
2015-06-13 15:01:05 +03:00
|
|
|
{{# def.error:'type' }}
|
|
|
|
}
|
2015-06-06 16:12:13 +03:00
|
|
|
{{?}}
|
|
|
|
|
2015-06-14 01:41:33 +03:00
|
|
|
{{? $breakOnError }} {{= $closingBraces2 }} {{?}}
|
2015-06-02 03:59:32 +03:00
|
|
|
|
2015-06-16 17:10:21 +03:00
|
|
|
{{? $top }}
|
2015-07-04 03:37:35 +03:00
|
|
|
validate.errors = vErrors; {{ /* don't edit, used in replace */ }}
|
|
|
|
return errors === 0; {{ /* don't edit, used in replace */ }}
|
2015-06-04 02:17:33 +03:00
|
|
|
}
|
2015-06-04 23:43:16 +03:00
|
|
|
{{??}}
|
2015-06-14 01:41:33 +03:00
|
|
|
var {{=$valid}} = errors === errs_{{=$lvl}};
|
2015-06-04 02:17:33 +03:00
|
|
|
{{?}}
|
|
|
|
|
2015-06-06 16:12:13 +03:00
|
|
|
{{# def.cleanUp }}
|
2015-05-31 01:04:46 +03:00
|
|
|
|
2015-06-16 17:10:21 +03:00
|
|
|
{{? $top && $breakOnError }}
|
2015-06-14 11:04:11 +03:00
|
|
|
{{# def.cleanUpVarErrors }}
|
|
|
|
{{?}}
|
|
|
|
|
2015-05-31 01:04:46 +03:00
|
|
|
{{
|
|
|
|
function $shouldUseGroup($rulesGroup) {
|
2015-10-10 22:50:57 +03:00
|
|
|
for (var i=0; i < $rulesGroup.rules.length; i++)
|
|
|
|
if ($shouldUseRule($rulesGroup.rules[i]))
|
|
|
|
return true;
|
2015-05-31 01:04:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function $shouldUseRule($rule) {
|
2015-06-14 01:41:33 +03:00
|
|
|
return it.schema[$rule.keyword] !== undefined ||
|
|
|
|
( $rule.keyword == 'properties' &&
|
2015-10-11 02:08:58 +03:00
|
|
|
( 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 01:04:46 +03:00
|
|
|
}
|
2015-05-31 14:21:41 +03:00
|
|
|
}}
|