2015-06-06 16:12:13 +03:00
{{# def.definitions }}
2015-12-12 16:41:51 +03:00
{{# def.errors }}
2016-01-09 16:28:40 +03:00
{{# def.defaults }}
2016-01-17 04:05:06 +03:00
{{# def.coerce }}
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
*/ }}
2016-01-25 01:55:19 +03:00
{{ var $async = it.schema.$async === true; }}
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;
2016-01-28 15:31:20 +03:00
if ($async) {
it.async = true;
2016-02-01 00:56:32 +03:00
var $es7 = it.opts.async == 'es7';
2016-01-28 15:31:20 +03:00
it.yieldAwait = $es7 ? 'await' : 'yield';
}
2015-06-16 17:10:21 +03:00
delete it.isTop;
2015-12-20 23:42:33 +03:00
2016-01-17 04:05:06 +03:00
it.dataPathArr = [undefined];
2015-06-02 03:59:32 +03:00
}}
2016-09-06 23:29:39 +03:00
var validate =
2016-01-28 15:31:20 +03:00
{{? $async }}
{{? $es7 }}
2016-01-31 01:13:00 +03:00
(async function
2016-01-28 15:31:20 +03:00
{{??}}
2016-02-01 00:56:32 +03:00
{{? it.opts.async == 'co*'}}co.wrap{{?}}(function*
2016-01-28 15:31:20 +03:00
{{?}}
{{??}}
2016-01-31 01:13:00 +03:00
(function
2016-01-28 15:31:20 +03:00
{{?}}
2016-08-29 23:51:49 +03:00
(data, dataPath, parentData, parentDataProperty, rootData) {
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 */ }}
2016-08-29 23:51:49 +03:00
if (rootData === undefined) rootData = data;
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);
2016-01-25 01:55:19 +03:00
2016-01-28 02:43:28 +03:00
if ($async && !it.async) throw new Error('async schema in sync schema');
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 = ''
2017-04-08 01:26:25 +03:00
, $closingBraces2 = '';
2015-06-06 16:12:13 +03:00
2017-04-08 01:26:25 +03:00
var $errorKeyword;
2016-01-17 04:05:06 +03:00
var $typeSchema = it.schema.type
, $typeIsArray = Array.isArray($typeSchema);
2015-06-03 03:36:59 +03:00
}}
2015-05-30 21:11:06 +03:00
2016-08-06 22:27:48 +03:00
{{## def.checkType:
2016-01-17 04:05:06 +03:00
{{
var $schemaPath = it.schemaPath + '.type'
, $errSchemaPath = it.errSchemaPath + '/type'
, $method = $typeIsArray ? 'checkDataTypes' : 'checkDataType';
}}
if ({{= it.util[$method]($typeSchema, $data, true) }}) {
#}}
2016-01-17 16:12:28 +03:00
{{? $typeSchema && it.opts.coerceTypes }}
2016-07-23 03:07:10 +03:00
{{ var $coerceToTypes = it.util.coerceToTypes(it.opts.coerceTypes, $typeSchema); }}
2016-01-17 16:12:28 +03:00
{{? $coerceToTypes }}
{{# def.checkType }}
{{# def.coerceType }}
}
{{?}}
2016-01-17 04:05:06 +03:00
{{?}}
2016-08-06 22:27:48 +03:00
{{ var $refKeywords; }}
2016-08-07 13:05:27 +03:00
{{? it.schema.$ref && ($refKeywords = it.util.schemaHasRulesExcept(it.schema, it.RULES.all, '$ref')) }}
2016-08-06 22:27:48 +03:00
{{? it.opts.extendRefs == 'fail' }}
2016-10-05 20:11:31 +03:00
{{ throw new Error('$ref: validation keywords used in schema at path "' + it.errSchemaPath + '"'); }}
2016-08-06 22:27:48 +03:00
{{?? it.opts.extendRefs == 'ignore' }}
{{
$refKeywords = false;
2016-10-05 20:11:31 +03:00
console.log('$ref: keywords ignored in schema at path "' + it.errSchemaPath + '"');
2016-08-06 22:27:48 +03:00
}}
2016-10-05 20:11:31 +03:00
{{?? it.opts.extendRefs !== true }}
2016-10-05 20:21:36 +03:00
{{ console.log('$ref: all keywords used in schema at path "' + it.errSchemaPath + '". It will change in the next major version, see issue #260. Use option { extendRefs: true } to keep current behaviour'); }}
2016-08-06 22:27:48 +03:00
{{?}}
{{?}}
{{? it.schema.$ref && !$refKeywords }}
2016-08-06 15:05:39 +03:00
{{= it.RULES.all.$ref.code(it, '$ref') }}
{{? $breakOnError }}
}
if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
{{ $closingBraces2 += '}'; }}
{{?}}
{{??}}
{{~ it.RULES:$rulesGroup }}
{{? $shouldUseGroup($rulesGroup) }}
{{? $rulesGroup.type }}
if ({{= it.util.checkDataType($rulesGroup.type, $data) }}) {
2016-01-09 16:28:40 +03:00
{{?}}
2016-08-06 15:05:39 +03:00
{{? it.opts.useDefaults && !it.compositeRule }}
{{? $rulesGroup.type == 'object' && it.schema.properties }}
{{# def.defaultProperties }}
{{?? $rulesGroup.type == 'array' && Array.isArray(it.schema.items) }}
{{# def.defaultItems }}
2015-05-31 01:04:46 +03:00
{{?}}
2015-06-02 03:59:32 +03:00
{{?}}
2016-08-06 15:05:39 +03:00
{{~ $rulesGroup.rules:$rule }}
{{? $shouldUseRule($rule) }}
{{= $rule.code(it, $rule.keyword) }}
{{? $breakOnError }}
{{ $closingBraces1 += '}'; }}
{{?}}
{{?}}
{{~}}
{{? $breakOnError }}
{{= $closingBraces1 }}
{{ $closingBraces1 = ''; }}
{{?}}
{{? $rulesGroup.type }}
2015-06-13 15:01:05 +03:00
}
2016-08-06 15:05:39 +03:00
{{? $typeSchema && $typeSchema === $rulesGroup.type }}
{{ var $typeChecked = true; }}
else {
{{
var $schemaPath = it.schemaPath + '.type'
, $errSchemaPath = it.errSchemaPath + '/type';
}}
{{# def.error:'type' }}
}
{{?}}
2015-06-06 16:12:13 +03:00
{{?}}
2015-06-04 23:43:16 +03:00
2016-08-06 15:05:39 +03:00
{{? $breakOnError }}
if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
{{ $closingBraces2 += '}'; }}
{{?}}
2015-05-30 21:11:06 +03:00
{{?}}
2016-08-06 15:05:39 +03:00
{{~}}
{{?}}
2015-05-30 21:11:06 +03:00
2016-01-17 16:12:28 +03:00
{{? $typeSchema && !$typeChecked && !(it.opts.coerceTypes && $coerceToTypes) }}
2016-01-17 04:05:06 +03:00
{{# def.checkType }}
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 }}
2016-01-25 01:55:19 +03:00
{{? $async }}
2016-01-28 02:43:28 +03:00
if (errors === 0) return true; {{ /* don't edit, used in replace */ }}
else throw new ValidationError(vErrors); {{ /* don't edit, used in replace */ }}
2016-01-25 01:55:19 +03:00
{{??}}
validate.errors = vErrors; {{ /* don't edit, used in replace */ }}
return errors === 0; {{ /* don't edit, used in replace */ }}
{{?}}
2016-01-31 01:13:00 +03:00
});
2016-09-06 23:29:39 +03:00
return validate;
2015-06-04 23:43:16 +03:00
{{??}}
2016-01-25 01:55:19 +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
}}