Initial commit
[yaffs-website] / node_modules / ajv / lib / dot / validate.jst
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 {{ var $async = it.schema.$async === true; }}
18
19 {{? it.isTop}}
20   {{
21     var $top = it.isTop
22       , $lvl = it.level = 0
23       , $dataLvl = it.dataLevel = 0
24       , $data = 'data';
25     it.rootId = it.resolve.fullPath(it.root.schema.id);
26     it.baseId = it.baseId || it.rootId;
27     if ($async) {
28       it.async = true;
29       var $es7 = it.opts.async == 'es7';
30       it.yieldAwait = $es7 ? 'await' : 'yield';
31     }
32     delete it.isTop;
33
34     it.dataPathArr = [undefined];
35   }}
36
37   var validate =
38   {{? $async }}
39     {{? $es7 }}
40       (async function
41     {{??}}
42       {{? it.opts.async == 'co*'}}co.wrap{{?}}(function*
43     {{?}}
44   {{??}}
45     (function
46   {{?}}
47     (data, dataPath, parentData, parentDataProperty, rootData) {
48     'use strict';
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;
52 {{??}}
53   {{
54     var $lvl = it.level
55       , $dataLvl = it.dataLevel
56       , $data = 'data' + ($dataLvl || '');
57
58     if (it.schema.id) it.baseId = it.resolve.url(it.baseId, it.schema.id);
59
60     if ($async && !it.async) throw new Error('async schema in sync schema');
61   }}
62
63   var errs_{{=$lvl}} = errors;
64 {{?}}
65
66 {{
67   var $valid = 'valid' + $lvl
68     , $breakOnError = !it.opts.allErrors
69     , $closingBraces1 = ''
70     , $closingBraces2 = ''
71     , $errorKeyword;
72
73   var $typeSchema = it.schema.type
74     , $typeIsArray = Array.isArray($typeSchema);
75 }}
76
77 {{## def.checkType:
78   {{
79     var $schemaPath = it.schemaPath + '.type'
80       , $errSchemaPath = it.errSchemaPath + '/type'
81       , $method = $typeIsArray ? 'checkDataTypes' : 'checkDataType';
82   }}
83
84   if ({{= it.util[$method]($typeSchema, $data, true) }}) {
85 #}}
86
87 {{? $typeSchema && it.opts.coerceTypes }}
88   {{ var $coerceToTypes = it.util.coerceToTypes(it.opts.coerceTypes, $typeSchema); }}
89   {{? $coerceToTypes }}
90     {{# def.checkType }}
91       {{# def.coerceType }}
92     }
93   {{?}}
94 {{?}}
95
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' }}
101     {{
102       $refKeywords = false;
103       console.log('$ref: keywords ignored in schema at path "' + it.errSchemaPath + '"');
104     }}
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'); }}
107   {{?}}
108 {{?}}
109
110 {{? it.schema.$ref && !$refKeywords }}
111   {{= it.RULES.all.$ref.code(it, '$ref') }}
112   {{? $breakOnError }}
113     }
114     if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
115     {{ $closingBraces2 += '}'; }}
116   {{?}}
117 {{??}}
118   {{~ it.RULES:$rulesGroup }}
119     {{? $shouldUseGroup($rulesGroup) }}
120       {{? $rulesGroup.type }}
121         if ({{= it.util.checkDataType($rulesGroup.type, $data) }}) {
122       {{?}}
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 }}
128           {{?}}
129         {{?}}
130         {{~ $rulesGroup.rules:$rule }}
131           {{? $shouldUseRule($rule) }}
132             {{= $rule.code(it, $rule.keyword) }}
133             {{? $breakOnError }}
134               {{ $closingBraces1 += '}'; }}
135             {{?}}
136           {{?}}
137         {{~}}
138         {{? $breakOnError }}
139           {{= $closingBraces1 }}
140           {{ $closingBraces1 = ''; }}
141         {{?}}
142       {{? $rulesGroup.type }}
143         }
144         {{? $typeSchema && $typeSchema === $rulesGroup.type }}
145           {{ var $typeChecked = true; }}
146           else {
147             {{
148               var $schemaPath = it.schemaPath + '.type'
149                 , $errSchemaPath = it.errSchemaPath + '/type';
150             }}
151             {{# def.error:'type' }}
152           }
153         {{?}}
154       {{?}}
155
156       {{? $breakOnError }}
157         if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
158         {{ $closingBraces2 += '}'; }}
159       {{?}}
160     {{?}}
161   {{~}}
162 {{?}}
163
164 {{? $typeSchema && !$typeChecked && !(it.opts.coerceTypes && $coerceToTypes) }}
165   {{# def.checkType }}
166     {{# def.error:'type' }}
167   }
168 {{?}}
169
170 {{? $breakOnError }} {{= $closingBraces2 }} {{?}}
171
172 {{? $top }}
173     {{? $async }}
174       if (errors === 0) return true;           {{ /* don't edit, used in replace */ }}
175       else throw new ValidationError(vErrors); {{ /* don't edit, used in replace */ }}
176     {{??}}
177       validate.errors = vErrors; {{ /* don't edit, used in replace */ }}
178       return errors === 0;       {{ /* don't edit, used in replace */ }}
179     {{?}}
180   });
181
182   return validate;
183 {{??}}
184   var {{=$valid}} = errors === errs_{{=$lvl}};
185 {{?}}
186
187 {{# def.cleanUp }}
188
189 {{? $top && $breakOnError }}
190   {{# def.cleanUpVarErrors }}
191 {{?}}
192
193 {{
194   function $shouldUseGroup($rulesGroup) {
195     for (var i=0; i < $rulesGroup.rules.length; i++)
196       if ($shouldUseRule($rulesGroup.rules[i]))
197         return true;
198   }
199
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 )));
209   }
210 }}