BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / ajv / lib / dot / validate.jst

validate.jst

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
validate.jst 7.1 KiB Download Raw
1{{# def.definitions }}
2{{# def.errors }}
3{{# def.defaults }}
4{{# def.coerce }}
5
6{{ /**
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
11 *
12 * runtime:
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
15 */ }}
16
17{{
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);
21}}
22
23{{? it.isTop }}
24 {{? $async }}
25 {{
26 it.async = true;
27 var $es7 = it.opts.async == 'es7';
28 it.yieldAwait = $es7 ? 'await' : 'yield';
29 }}
30 {{?}}
31
32 var validate =
33 {{? $async }}
34 {{? $es7 }}
35 (async function
36 {{??}}
37 {{? it.opts.async != '*'}}co.wrap{{?}}(function*
38 {{?}}
39 {{??}}
40 (function
41 {{?}}
42 (data, dataPath, parentData, parentDataProperty, rootData) {
43 'use strict';
44 {{? $id && (it.opts.sourceCode || it.opts.processCode) }}
45 {{= '/\*# sourceURL=' + $id + ' */' }}
46 {{?}}
47{{?}}
48
49{{? typeof it.schema == 'boolean' || !($refKeywords || it.schema.$ref) }}
50 {{ var $keyword = 'false schema'; }}
51 {{# def.setupKeyword }}
52 {{? it.schema === false}}
53 {{? it.isTop}}
54 {{ $breakOnError = true; }}
55 {{??}}
56 var {{=$valid}} = false;
57 {{?}}
58 {{# def.error:'false schema' }}
59 {{??}}
60 {{? it.isTop}}
61 {{? $async }}
62 return data;
63 {{??}}
64 validate.errors = null;
65 return true;
66 {{?}}
67 {{??}}
68 var {{=$valid}} = true;
69 {{?}}
70 {{?}}
71
72 {{? it.isTop}}
73 });
74 return validate;
75 {{?}}
76
77 {{ return out; }}
78{{?}}
79
80
81{{? it.isTop }}
82 {{
83 var $top = it.isTop
84 , $lvl = it.level = 0
85 , $dataLvl = it.dataLevel = 0
86 , $data = 'data';
87 it.rootId = it.resolve.fullPath(it.self._getId(it.root.schema));
88 it.baseId = it.baseId || it.rootId;
89 delete it.isTop;
90
91 it.dataPathArr = [undefined];
92 }}
93
94 var vErrors = null; {{ /* don't edit, used in replace */ }}
95 var errors = 0; {{ /* don't edit, used in replace */ }}
96 if (rootData === undefined) rootData = data; {{ /* don't edit, used in replace */ }}
97{{??}}
98 {{
99 var $lvl = it.level
100 , $dataLvl = it.dataLevel
101 , $data = 'data' + ($dataLvl || '');
102
103 if ($id) it.baseId = it.resolve.url(it.baseId, $id);
104
105 if ($async && !it.async) throw new Error('async schema in sync schema');
106 }}
107
108 var errs_{{=$lvl}} = errors;
109{{?}}
110
111{{
112 var $valid = 'valid' + $lvl
113 , $breakOnError = !it.opts.allErrors
114 , $closingBraces1 = ''
115 , $closingBraces2 = '';
116
117 var $errorKeyword;
118 var $typeSchema = it.schema.type
119 , $typeIsArray = Array.isArray($typeSchema);
120
121 if ($typeIsArray && $typeSchema.length == 1) {
122 $typeSchema = $typeSchema[0];
123 $typeIsArray = false;
124 }
125}}
126
127{{## def.checkType:
128 {{
129 var $schemaPath = it.schemaPath + '.type'
130 , $errSchemaPath = it.errSchemaPath + '/type'
131 , $method = $typeIsArray ? 'checkDataTypes' : 'checkDataType';
132 }}
133
134 if ({{= it.util[$method]($typeSchema, $data, true) }}) {
135#}}
136
137{{? it.schema.$ref && $refKeywords }}
138 {{? it.opts.extendRefs == 'fail' }}
139 {{ throw new Error('$ref: validation keywords used in schema at path "' + it.errSchemaPath + '" (see option extendRefs)'); }}
140 {{?? it.opts.extendRefs !== true }}
141 {{
142 $refKeywords = false;
143 it.logger.warn('$ref: keywords ignored in schema at path "' + it.errSchemaPath + '"');
144 }}
145 {{?}}
146{{?}}
147
148{{? $typeSchema }}
149 {{? it.opts.coerceTypes }}
150 {{ var $coerceToTypes = it.util.coerceToTypes(it.opts.coerceTypes, $typeSchema); }}
151 {{?}}
152
153 {{ var $rulesGroup = it.RULES.types[$typeSchema]; }}
154 {{? $coerceToTypes || $typeIsArray || $rulesGroup === true ||
155 ($rulesGroup && !$shouldUseGroup($rulesGroup)) }}
156 {{
157 var $schemaPath = it.schemaPath + '.type'
158 , $errSchemaPath = it.errSchemaPath + '/type';
159 }}
160 {{# def.checkType }}
161 {{? $coerceToTypes }}
162 {{# def.coerceType }}
163 {{??}}
164 {{# def.error:'type' }}
165 {{?}}
166 }
167 {{?}}
168{{?}}
169
170
171{{? it.schema.$ref && !$refKeywords }}
172 {{= it.RULES.all.$ref.code(it, '$ref') }}
173 {{? $breakOnError }}
174 }
175 if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
176 {{ $closingBraces2 += '}'; }}
177 {{?}}
178{{??}}
179 {{? it.opts.v5 && it.schema.patternGroups }}
180 {{ it.logger.warn('keyword "patternGroups" is deprecated and disabled. Use option patternGroups: true to enable.'); }}
181 {{?}}
182 {{~ it.RULES:$rulesGroup }}
183 {{? $shouldUseGroup($rulesGroup) }}
184 {{? $rulesGroup.type }}
185 if ({{= it.util.checkDataType($rulesGroup.type, $data) }}) {
186 {{?}}
187 {{? it.opts.useDefaults && !it.compositeRule }}
188 {{? $rulesGroup.type == 'object' && it.schema.properties }}
189 {{# def.defaultProperties }}
190 {{?? $rulesGroup.type == 'array' && Array.isArray(it.schema.items) }}
191 {{# def.defaultItems }}
192 {{?}}
193 {{?}}
194 {{~ $rulesGroup.rules:$rule }}
195 {{? $shouldUseRule($rule) }}
196 {{ var $code = $rule.code(it, $rule.keyword, $rulesGroup.type); }}
197 {{? $code }}
198 {{= $code }}
199 {{? $breakOnError }}
200 {{ $closingBraces1 += '}'; }}
201 {{?}}
202 {{?}}
203 {{?}}
204 {{~}}
205 {{? $breakOnError }}
206 {{= $closingBraces1 }}
207 {{ $closingBraces1 = ''; }}
208 {{?}}
209 {{? $rulesGroup.type }}
210 }
211 {{? $typeSchema && $typeSchema === $rulesGroup.type && !$coerceToTypes }}
212 else {
213 {{
214 var $schemaPath = it.schemaPath + '.type'
215 , $errSchemaPath = it.errSchemaPath + '/type';
216 }}
217 {{# def.error:'type' }}
218 }
219 {{?}}
220 {{?}}
221
222 {{? $breakOnError }}
223 if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
224 {{ $closingBraces2 += '}'; }}
225 {{?}}
226 {{?}}
227 {{~}}
228{{?}}
229
230{{? $breakOnError }} {{= $closingBraces2 }} {{?}}
231
232{{? $top }}
233 {{? $async }}
234 if (errors === 0) return data; {{ /* don't edit, used in replace */ }}
235 else throw new ValidationError(vErrors); {{ /* don't edit, used in replace */ }}
236 {{??}}
237 validate.errors = vErrors; {{ /* don't edit, used in replace */ }}
238 return errors === 0; {{ /* don't edit, used in replace */ }}
239 {{?}}
240 });
241
242 return validate;
243{{??}}
244 var {{=$valid}} = errors === errs_{{=$lvl}};
245{{?}}
246
247{{# def.cleanUp }}
248
249{{? $top }}
250 {{# def.finalCleanUp }}
251{{?}}
252
253{{
254 function $shouldUseGroup($rulesGroup) {
255 var rules = $rulesGroup.rules;
256 for (var i=0; i < rules.length; i++)
257 if ($shouldUseRule(rules[i]))
258 return true;
259 }
260
261 function $shouldUseRule($rule) {
262 return it.schema[$rule.keyword] !== undefined ||
263 ($rule.implements && $ruleImplementsSomeKeyword($rule));
264 }
265
266 function $ruleImplementsSomeKeyword($rule) {
267 var impl = $rule.implements;
268 for (var i=0; i < impl.length; i++)
269 if (it.schema[impl[i]] !== undefined)
270 return true;
271 }
272}}