2015-06-06 16:12:13 +03:00
|
|
|
{{# def.definitions }}
|
|
|
|
|
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-06-18 22:48:35 +03:00
|
|
|
it.rootId = it.baseId = it.resolve.fullPath(it.root.schema.id);
|
2015-06-16 17:10:21 +03:00
|
|
|
delete it.isTop;
|
|
|
|
it.wasTop = true;
|
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-06-14 11:04:11 +03:00
|
|
|
validate.errors = 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-06-13 13:40:45 +03:00
|
|
|
{{? it.opts._debug }} console.log('validate dataPath:', dataPath); {{?}}
|
2015-06-12 20:16:54 +03:00
|
|
|
|
2015-06-03 03:36:59 +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
|
|
|
|
|
|
|
if (it.schema.id) it.baseId = it.resolve.url(it.baseId, it.schema.id);
|
2015-06-16 17:10:21 +03:00
|
|
|
delete it.wasTop;
|
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 = ''
|
|
|
|
, $closingBraces2 = '';
|
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) }}
|
|
|
|
{{= $rule.code(it) }}
|
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-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'
|
|
|
|
, $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-06-14 11:04:11 +03:00
|
|
|
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) {
|
|
|
|
return $rulesGroup.rules.some(function ($rule) {
|
|
|
|
return $shouldUseRule($rule);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function $shouldUseRule($rule) {
|
2015-06-14 01:41:33 +03:00
|
|
|
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 01:04:46 +03:00
|
|
|
}
|
2015-05-31 14:21:41 +03:00
|
|
|
}}
|