7 * schema compilation (render) time:
8 * it = { schema, RULES, _validate, opts }
9 * it.validate - this template function,
10 * it is used recursively to generate code for subschemas
13 * "validate" is a variable name to which this function will be assigned
14 * validateRef etc. are defined in the parent scope in index.js
18 var $async = it.schema.$async === true
19 , $refKeywords = it.util.schemaHasRulesExcept(it.schema, it.RULES.all, '$ref')
20 , $id = it.self._getId(it.schema);
24 if (it.opts.strictKeywords) {
25 var $unknownKwd = it.util.schemaUnknownRules(it.schema, it.RULES.keywords);
27 var $keywordsMsg = 'unknown keyword: ' + $unknownKwd;
28 if (it.opts.strictKeywords === 'log') it.logger.warn($keywordsMsg);
29 else throw new Error($keywordsMsg);
35 var validate = {{?$async}}{{it.async = true;}}async {{?}}function(data, dataPath, parentData, parentDataProperty, rootData) {
37 {{? $id && (it.opts.sourceCode || it.opts.processCode) }}
38 {{= '/\*# sourceURL=' + $id + ' */' }}
42 {{? typeof it.schema == 'boolean' || !($refKeywords || it.schema.$ref) }}
43 {{ var $keyword = 'false schema'; }}
44 {{# def.setupKeyword }}
45 {{? it.schema === false}}
47 {{ $breakOnError = true; }}
49 var {{=$valid}} = false;
51 {{# def.error:'false schema' }}
57 validate.errors = null;
61 var {{=$valid}} = true;
78 , $dataLvl = it.dataLevel = 0
80 it.rootId = it.resolve.fullPath(it.self._getId(it.root.schema));
81 it.baseId = it.baseId || it.rootId;
84 it.dataPathArr = [undefined];
86 if (it.schema.default !== undefined && it.opts.useDefaults && it.opts.strictDefaults) {
87 var $defaultMsg = 'default is ignored in the schema root';
88 if (it.opts.strictDefaults === 'log') it.logger.warn($defaultMsg);
89 else throw new Error($defaultMsg);
93 var vErrors = null; {{ /* don't edit, used in replace */ }}
94 var errors = 0; {{ /* don't edit, used in replace */ }}
95 if (rootData === undefined) rootData = data; {{ /* don't edit, used in replace */ }}
99 , $dataLvl = it.dataLevel
100 , $data = 'data' + ($dataLvl || '');
102 if ($id) it.baseId = it.resolve.url(it.baseId, $id);
104 if ($async && !it.async) throw new Error('async schema in sync schema');
107 var errs_{{=$lvl}} = errors;
111 var $valid = 'valid' + $lvl
112 , $breakOnError = !it.opts.allErrors
113 , $closingBraces1 = ''
114 , $closingBraces2 = '';
117 var $typeSchema = it.schema.type
118 , $typeIsArray = Array.isArray($typeSchema);
120 if ($typeSchema && it.opts.nullable && it.schema.nullable === true) {
122 if ($typeSchema.indexOf('null') == -1)
123 $typeSchema = $typeSchema.concat('null');
124 } else if ($typeSchema != 'null') {
125 $typeSchema = [$typeSchema, 'null'];
130 if ($typeIsArray && $typeSchema.length == 1) {
131 $typeSchema = $typeSchema[0];
132 $typeIsArray = false;
138 var $schemaPath = it.schemaPath + '.type'
139 , $errSchemaPath = it.errSchemaPath + '/type'
140 , $method = $typeIsArray ? 'checkDataTypes' : 'checkDataType';
143 if ({{= it.util[$method]($typeSchema, $data, it.opts.strictNumbers, true) }}) {
146 {{? it.schema.$ref && $refKeywords }}
147 {{? it.opts.extendRefs == 'fail' }}
148 {{ throw new Error('$ref: validation keywords used in schema at path "' + it.errSchemaPath + '" (see option extendRefs)'); }}
149 {{?? it.opts.extendRefs !== true }}
151 $refKeywords = false;
152 it.logger.warn('$ref: keywords ignored in schema at path "' + it.errSchemaPath + '"');
157 {{? it.schema.$comment && it.opts.$comment }}
158 {{= it.RULES.all.$comment.code(it, '$comment') }}
162 {{? it.opts.coerceTypes }}
163 {{ var $coerceToTypes = it.util.coerceToTypes(it.opts.coerceTypes, $typeSchema); }}
166 {{ var $rulesGroup = it.RULES.types[$typeSchema]; }}
167 {{? $coerceToTypes || $typeIsArray || $rulesGroup === true ||
168 ($rulesGroup && !$shouldUseGroup($rulesGroup)) }}
170 var $schemaPath = it.schemaPath + '.type'
171 , $errSchemaPath = it.errSchemaPath + '/type';
174 {{? $coerceToTypes }}
175 {{# def.coerceType }}
177 {{# def.error:'type' }}
184 {{? it.schema.$ref && !$refKeywords }}
185 {{= it.RULES.all.$ref.code(it, '$ref') }}
188 if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
189 {{ $closingBraces2 += '}'; }}
192 {{~ it.RULES:$rulesGroup }}
193 {{? $shouldUseGroup($rulesGroup) }}
194 {{? $rulesGroup.type }}
195 if ({{= it.util.checkDataType($rulesGroup.type, $data, it.opts.strictNumbers) }}) {
197 {{? it.opts.useDefaults }}
198 {{? $rulesGroup.type == 'object' && it.schema.properties }}
199 {{# def.defaultProperties }}
200 {{?? $rulesGroup.type == 'array' && Array.isArray(it.schema.items) }}
201 {{# def.defaultItems }}
204 {{~ $rulesGroup.rules:$rule }}
205 {{? $shouldUseRule($rule) }}
206 {{ var $code = $rule.code(it, $rule.keyword, $rulesGroup.type); }}
210 {{ $closingBraces1 += '}'; }}
216 {{= $closingBraces1 }}
217 {{ $closingBraces1 = ''; }}
219 {{? $rulesGroup.type }}
221 {{? $typeSchema && $typeSchema === $rulesGroup.type && !$coerceToTypes }}
224 var $schemaPath = it.schemaPath + '.type'
225 , $errSchemaPath = it.errSchemaPath + '/type';
227 {{# def.error:'type' }}
233 if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
234 {{ $closingBraces2 += '}'; }}
240 {{? $breakOnError }} {{= $closingBraces2 }} {{?}}
244 if (errors === 0) return data; {{ /* don't edit, used in replace */ }}
245 else throw new ValidationError(vErrors); {{ /* don't edit, used in replace */ }}
247 validate.errors = vErrors; {{ /* don't edit, used in replace */ }}
248 return errors === 0; {{ /* don't edit, used in replace */ }}
254 var {{=$valid}} = errors === errs_{{=$lvl}};
258 function $shouldUseGroup($rulesGroup) {
259 var rules = $rulesGroup.rules;
260 for (var i=0; i < rules.length; i++)
261 if ($shouldUseRule(rules[i]))
265 function $shouldUseRule($rule) {
266 return it.schema[$rule.keyword] !== undefined ||
267 ($rule.implements && $ruleImplementsSomeKeyword($rule));
270 function $ruleImplementsSomeKeyword($rule) {
271 var impl = $rule.implements;
272 for (var i=0; i < impl.length; i++)
273 if (it.schema[impl[i]] !== undefined)