source: imaps-frontend/node_modules/ajv/lib/dotjs/required.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 12.1 KB
Line 
1'use strict';
2module.exports = function generate_required(it, $keyword, $ruleType) {
3 var out = ' ';
4 var $lvl = it.level;
5 var $dataLvl = it.dataLevel;
6 var $schema = it.schema[$keyword];
7 var $schemaPath = it.schemaPath + it.util.getProperty($keyword);
8 var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
9 var $breakOnError = !it.opts.allErrors;
10 var $data = 'data' + ($dataLvl || '');
11 var $valid = 'valid' + $lvl;
12 var $isData = it.opts.$data && $schema && $schema.$data,
13 $schemaValue;
14 if ($isData) {
15 out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
16 $schemaValue = 'schema' + $lvl;
17 } else {
18 $schemaValue = $schema;
19 }
20 var $vSchema = 'schema' + $lvl;
21 if (!$isData) {
22 if ($schema.length < it.opts.loopRequired && it.schema.properties && Object.keys(it.schema.properties).length) {
23 var $required = [];
24 var arr1 = $schema;
25 if (arr1) {
26 var $property, i1 = -1,
27 l1 = arr1.length - 1;
28 while (i1 < l1) {
29 $property = arr1[i1 += 1];
30 var $propertySch = it.schema.properties[$property];
31 if (!($propertySch && (it.opts.strictKeywords ? (typeof $propertySch == 'object' && Object.keys($propertySch).length > 0) || $propertySch === false : it.util.schemaHasRules($propertySch, it.RULES.all)))) {
32 $required[$required.length] = $property;
33 }
34 }
35 }
36 } else {
37 var $required = $schema;
38 }
39 }
40 if ($isData || $required.length) {
41 var $currentErrorPath = it.errorPath,
42 $loopRequired = $isData || $required.length >= it.opts.loopRequired,
43 $ownProperties = it.opts.ownProperties;
44 if ($breakOnError) {
45 out += ' var missing' + ($lvl) + '; ';
46 if ($loopRequired) {
47 if (!$isData) {
48 out += ' var ' + ($vSchema) + ' = validate.schema' + ($schemaPath) + '; ';
49 }
50 var $i = 'i' + $lvl,
51 $propertyPath = 'schema' + $lvl + '[' + $i + ']',
52 $missingProperty = '\' + ' + $propertyPath + ' + \'';
53 if (it.opts._errorDataPathProperty) {
54 it.errorPath = it.util.getPathExpr($currentErrorPath, $propertyPath, it.opts.jsonPointers);
55 }
56 out += ' var ' + ($valid) + ' = true; ';
57 if ($isData) {
58 out += ' if (schema' + ($lvl) + ' === undefined) ' + ($valid) + ' = true; else if (!Array.isArray(schema' + ($lvl) + ')) ' + ($valid) + ' = false; else {';
59 }
60 out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < ' + ($vSchema) + '.length; ' + ($i) + '++) { ' + ($valid) + ' = ' + ($data) + '[' + ($vSchema) + '[' + ($i) + ']] !== undefined ';
61 if ($ownProperties) {
62 out += ' && Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($vSchema) + '[' + ($i) + ']) ';
63 }
64 out += '; if (!' + ($valid) + ') break; } ';
65 if ($isData) {
66 out += ' } ';
67 }
68 out += ' if (!' + ($valid) + ') { ';
69 var $$outStack = $$outStack || [];
70 $$outStack.push(out);
71 out = ''; /* istanbul ignore else */
72 if (it.createErrors !== false) {
73 out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
74 if (it.opts.messages !== false) {
75 out += ' , message: \'';
76 if (it.opts._errorDataPathProperty) {
77 out += 'is a required property';
78 } else {
79 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
80 }
81 out += '\' ';
82 }
83 if (it.opts.verbose) {
84 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
85 }
86 out += ' } ';
87 } else {
88 out += ' {} ';
89 }
90 var __err = out;
91 out = $$outStack.pop();
92 if (!it.compositeRule && $breakOnError) {
93 /* istanbul ignore if */
94 if (it.async) {
95 out += ' throw new ValidationError([' + (__err) + ']); ';
96 } else {
97 out += ' validate.errors = [' + (__err) + ']; return false; ';
98 }
99 } else {
100 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
101 }
102 out += ' } else { ';
103 } else {
104 out += ' if ( ';
105 var arr2 = $required;
106 if (arr2) {
107 var $propertyKey, $i = -1,
108 l2 = arr2.length - 1;
109 while ($i < l2) {
110 $propertyKey = arr2[$i += 1];
111 if ($i) {
112 out += ' || ';
113 }
114 var $prop = it.util.getProperty($propertyKey),
115 $useData = $data + $prop;
116 out += ' ( ( ' + ($useData) + ' === undefined ';
117 if ($ownProperties) {
118 out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
119 }
120 out += ') && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString(it.opts.jsonPointers ? $propertyKey : $prop)) + ') ) ';
121 }
122 }
123 out += ') { ';
124 var $propertyPath = 'missing' + $lvl,
125 $missingProperty = '\' + ' + $propertyPath + ' + \'';
126 if (it.opts._errorDataPathProperty) {
127 it.errorPath = it.opts.jsonPointers ? it.util.getPathExpr($currentErrorPath, $propertyPath, true) : $currentErrorPath + ' + ' + $propertyPath;
128 }
129 var $$outStack = $$outStack || [];
130 $$outStack.push(out);
131 out = ''; /* istanbul ignore else */
132 if (it.createErrors !== false) {
133 out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
134 if (it.opts.messages !== false) {
135 out += ' , message: \'';
136 if (it.opts._errorDataPathProperty) {
137 out += 'is a required property';
138 } else {
139 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
140 }
141 out += '\' ';
142 }
143 if (it.opts.verbose) {
144 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
145 }
146 out += ' } ';
147 } else {
148 out += ' {} ';
149 }
150 var __err = out;
151 out = $$outStack.pop();
152 if (!it.compositeRule && $breakOnError) {
153 /* istanbul ignore if */
154 if (it.async) {
155 out += ' throw new ValidationError([' + (__err) + ']); ';
156 } else {
157 out += ' validate.errors = [' + (__err) + ']; return false; ';
158 }
159 } else {
160 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
161 }
162 out += ' } else { ';
163 }
164 } else {
165 if ($loopRequired) {
166 if (!$isData) {
167 out += ' var ' + ($vSchema) + ' = validate.schema' + ($schemaPath) + '; ';
168 }
169 var $i = 'i' + $lvl,
170 $propertyPath = 'schema' + $lvl + '[' + $i + ']',
171 $missingProperty = '\' + ' + $propertyPath + ' + \'';
172 if (it.opts._errorDataPathProperty) {
173 it.errorPath = it.util.getPathExpr($currentErrorPath, $propertyPath, it.opts.jsonPointers);
174 }
175 if ($isData) {
176 out += ' if (' + ($vSchema) + ' && !Array.isArray(' + ($vSchema) + ')) { var err = '; /* istanbul ignore else */
177 if (it.createErrors !== false) {
178 out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
179 if (it.opts.messages !== false) {
180 out += ' , message: \'';
181 if (it.opts._errorDataPathProperty) {
182 out += 'is a required property';
183 } else {
184 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
185 }
186 out += '\' ';
187 }
188 if (it.opts.verbose) {
189 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
190 }
191 out += ' } ';
192 } else {
193 out += ' {} ';
194 }
195 out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } else if (' + ($vSchema) + ' !== undefined) { ';
196 }
197 out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < ' + ($vSchema) + '.length; ' + ($i) + '++) { if (' + ($data) + '[' + ($vSchema) + '[' + ($i) + ']] === undefined ';
198 if ($ownProperties) {
199 out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($vSchema) + '[' + ($i) + ']) ';
200 }
201 out += ') { var err = '; /* istanbul ignore else */
202 if (it.createErrors !== false) {
203 out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
204 if (it.opts.messages !== false) {
205 out += ' , message: \'';
206 if (it.opts._errorDataPathProperty) {
207 out += 'is a required property';
208 } else {
209 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
210 }
211 out += '\' ';
212 }
213 if (it.opts.verbose) {
214 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
215 }
216 out += ' } ';
217 } else {
218 out += ' {} ';
219 }
220 out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } } ';
221 if ($isData) {
222 out += ' } ';
223 }
224 } else {
225 var arr3 = $required;
226 if (arr3) {
227 var $propertyKey, i3 = -1,
228 l3 = arr3.length - 1;
229 while (i3 < l3) {
230 $propertyKey = arr3[i3 += 1];
231 var $prop = it.util.getProperty($propertyKey),
232 $missingProperty = it.util.escapeQuotes($propertyKey),
233 $useData = $data + $prop;
234 if (it.opts._errorDataPathProperty) {
235 it.errorPath = it.util.getPath($currentErrorPath, $propertyKey, it.opts.jsonPointers);
236 }
237 out += ' if ( ' + ($useData) + ' === undefined ';
238 if ($ownProperties) {
239 out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
240 }
241 out += ') { var err = '; /* istanbul ignore else */
242 if (it.createErrors !== false) {
243 out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
244 if (it.opts.messages !== false) {
245 out += ' , message: \'';
246 if (it.opts._errorDataPathProperty) {
247 out += 'is a required property';
248 } else {
249 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
250 }
251 out += '\' ';
252 }
253 if (it.opts.verbose) {
254 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
255 }
256 out += ' } ';
257 } else {
258 out += ' {} ';
259 }
260 out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } ';
261 }
262 }
263 }
264 }
265 it.errorPath = $currentErrorPath;
266 } else if ($breakOnError) {
267 out += ' if (true) {';
268 }
269 return out;
270}
Note: See TracBrowser for help on using the repository browser.