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
17 {{ var $async = it.schema.$async === true; }}
23 , $dataLvl = it.dataLevel = 0
25 it.rootId = it.resolve.fullPath(it.root.schema.id);
26 it.baseId = it.baseId || it.rootId;
29 var $es7 = it.opts.async == 'es7';
30 it.yieldAwait = $es7 ? 'await' : 'yield';
34 it.dataPathArr = [undefined];
42 {{? it.opts.async == 'co*'}}co.wrap{{?}}(function*
47 (data, dataPath, parentData, parentDataProperty, rootData) {
49 var vErrors = null; {{ /* don't edit, used in replace */ }}
50 var errors = 0; {{ /* don't edit, used in replace */ }}
51 if (rootData === undefined) rootData = data;
55 , $dataLvl = it.dataLevel
56 , $data = 'data' + ($dataLvl || '');
58 if (it.schema.id) it.baseId = it.resolve.url(it.baseId, it.schema.id);
60 if ($async && !it.async) throw new Error('async schema in sync schema');
63 var errs_{{=$lvl}} = errors;
67 var $valid = 'valid' + $lvl
68 , $breakOnError = !it.opts.allErrors
69 , $closingBraces1 = ''
70 , $closingBraces2 = ''
73 var $typeSchema = it.schema.type
74 , $typeIsArray = Array.isArray($typeSchema);
79 var $schemaPath = it.schemaPath + '.type'
80 , $errSchemaPath = it.errSchemaPath + '/type'
81 , $method = $typeIsArray ? 'checkDataTypes' : 'checkDataType';
84 if ({{= it.util[$method]($typeSchema, $data, true) }}) {
87 {{? $typeSchema && it.opts.coerceTypes }}
88 {{ var $coerceToTypes = it.util.coerceToTypes(it.opts.coerceTypes, $typeSchema); }}
96 {{ var $refKeywords; }}
97 {{? it.schema.$ref && ($refKeywords = it.util.schemaHasRulesExcept(it.schema, it.RULES.all, '$ref')) }}
98 {{? it.opts.extendRefs == 'fail' }}
99 {{ throw new Error('$ref: validation keywords used in schema at path "' + it.errSchemaPath + '"'); }}
100 {{?? it.opts.extendRefs == 'ignore' }}
102 $refKeywords = false;
103 console.log('$ref: keywords ignored in schema at path "' + it.errSchemaPath + '"');
105 {{?? it.opts.extendRefs !== true }}
106 {{ 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'); }}
110 {{? it.schema.$ref && !$refKeywords }}
111 {{= it.RULES.all.$ref.code(it, '$ref') }}
114 if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
115 {{ $closingBraces2 += '}'; }}
118 {{~ it.RULES:$rulesGroup }}
119 {{? $shouldUseGroup($rulesGroup) }}
120 {{? $rulesGroup.type }}
121 if ({{= it.util.checkDataType($rulesGroup.type, $data) }}) {
123 {{? it.opts.useDefaults && !it.compositeRule }}
124 {{? $rulesGroup.type == 'object' && it.schema.properties }}
125 {{# def.defaultProperties }}
126 {{?? $rulesGroup.type == 'array' && Array.isArray(it.schema.items) }}
127 {{# def.defaultItems }}
130 {{~ $rulesGroup.rules:$rule }}
131 {{? $shouldUseRule($rule) }}
132 {{= $rule.code(it, $rule.keyword) }}
134 {{ $closingBraces1 += '}'; }}
139 {{= $closingBraces1 }}
140 {{ $closingBraces1 = ''; }}
142 {{? $rulesGroup.type }}
144 {{? $typeSchema && $typeSchema === $rulesGroup.type }}
145 {{ var $typeChecked = true; }}
148 var $schemaPath = it.schemaPath + '.type'
149 , $errSchemaPath = it.errSchemaPath + '/type';
151 {{# def.error:'type' }}
157 if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
158 {{ $closingBraces2 += '}'; }}
164 {{? $typeSchema && !$typeChecked && !(it.opts.coerceTypes && $coerceToTypes) }}
166 {{# def.error:'type' }}
170 {{? $breakOnError }} {{= $closingBraces2 }} {{?}}
174 if (errors === 0) return true; {{ /* don't edit, used in replace */ }}
175 else throw new ValidationError(vErrors); {{ /* don't edit, used in replace */ }}
177 validate.errors = vErrors; {{ /* don't edit, used in replace */ }}
178 return errors === 0; {{ /* don't edit, used in replace */ }}
184 var {{=$valid}} = errors === errs_{{=$lvl}};
189 {{? $top && $breakOnError }}
190 {{# def.cleanUpVarErrors }}
194 function $shouldUseGroup($rulesGroup) {
195 for (var i=0; i < $rulesGroup.rules.length; i++)
196 if ($shouldUseRule($rulesGroup.rules[i]))
200 function $shouldUseRule($rule) {
201 return it.schema[$rule.keyword] !== undefined ||
202 ( $rule.keyword == 'properties' &&
203 ( it.schema.additionalProperties === false ||
204 typeof it.schema.additionalProperties == 'object'
205 || ( it.schema.patternProperties &&
206 Object.keys(it.schema.patternProperties).length )
207 || ( it.opts.v5 && it.schema.patternGroups &&
208 Object.keys(it.schema.patternGroups).length )));