2 module.exports = function generate_properties(it, $keyword) {
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;
11 var $data = 'data' + ($dataLvl || '');
12 var $valid = 'valid' + $lvl;
13 var $errs = 'errs__' + $lvl;
14 var $it = it.util.copy(it);
15 var $closingBraces = '';
17 var $nextValid = 'valid' + $it.level;
18 var $key = 'key' + $lvl,
19 $dataNxt = $it.dataLevel = it.dataLevel + 1,
20 $nextData = 'data' + $dataNxt;
21 var $schemaKeys = Object.keys($schema || {}),
22 $pProperties = it.schema.patternProperties || {},
23 $pPropertyKeys = Object.keys($pProperties),
24 $aProperties = it.schema.additionalProperties,
25 $someProperties = $schemaKeys.length || $pPropertyKeys.length,
26 $noAdditional = $aProperties === false,
27 $additionalIsSchema = typeof $aProperties == 'object' && Object.keys($aProperties).length,
28 $removeAdditional = it.opts.removeAdditional,
29 $checkAdditional = $noAdditional || $additionalIsSchema || $removeAdditional,
30 $ownProperties = it.opts.ownProperties,
31 $currentBaseId = it.baseId;
32 var $required = it.schema.required;
33 if ($required && !(it.opts.v5 && $required.$data) && $required.length < it.opts.loopRequired) var $requiredHash = it.util.toHash($required);
35 var $pgProperties = it.schema.patternGroups || {},
36 $pgPropertyKeys = Object.keys($pgProperties);
38 out += 'var ' + ($errs) + ' = errors;var ' + ($nextValid) + ' = true;';
39 if ($checkAdditional) {
40 out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
42 out += ' if (!Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($key) + ')) continue; ';
44 if ($someProperties) {
45 out += ' var isAdditional' + ($lvl) + ' = !(false ';
46 if ($schemaKeys.length) {
47 if ($schemaKeys.length > 5) {
48 out += ' || validate.schema' + ($schemaPath) + '[' + ($key) + '] ';
50 var arr1 = $schemaKeys;
52 var $propertyKey, i1 = -1,
55 $propertyKey = arr1[i1 += 1];
56 out += ' || ' + ($key) + ' == ' + (it.util.toQuotedString($propertyKey)) + ' ';
61 if ($pPropertyKeys.length) {
62 var arr2 = $pPropertyKeys;
64 var $pProperty, $i = -1,
67 $pProperty = arr2[$i += 1];
68 out += ' || ' + (it.usePattern($pProperty)) + '.test(' + ($key) + ') ';
72 if (it.opts.v5 && $pgPropertyKeys && $pgPropertyKeys.length) {
73 var arr3 = $pgPropertyKeys;
75 var $pgProperty, $i = -1,
78 $pgProperty = arr3[$i += 1];
79 out += ' || ' + (it.usePattern($pgProperty)) + '.test(' + ($key) + ') ';
83 out += ' ); if (isAdditional' + ($lvl) + ') { ';
85 if ($removeAdditional == 'all') {
86 out += ' delete ' + ($data) + '[' + ($key) + ']; ';
88 var $currentErrorPath = it.errorPath;
89 var $additionalProperty = '\' + ' + $key + ' + \'';
90 if (it.opts._errorDataPathProperty) {
91 it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
94 if ($removeAdditional) {
95 out += ' delete ' + ($data) + '[' + ($key) + ']; ';
97 out += ' ' + ($nextValid) + ' = false; ';
98 var $currErrSchemaPath = $errSchemaPath;
99 $errSchemaPath = it.errSchemaPath + '/additionalProperties';
100 var $$outStack = $$outStack || [];
101 $$outStack.push(out);
102 out = ''; /* istanbul ignore else */
103 if (it.createErrors !== false) {
104 out += ' { keyword: \'' + ($errorKeyword || 'additionalProperties') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { additionalProperty: \'' + ($additionalProperty) + '\' } ';
105 if (it.opts.messages !== false) {
106 out += ' , message: \'should NOT have additional properties\' ';
108 if (it.opts.verbose) {
109 out += ' , schema: false , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
116 out = $$outStack.pop();
117 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
119 out += ' throw new ValidationError([' + (__err) + ']); ';
121 out += ' validate.errors = [' + (__err) + ']; return false; ';
124 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
126 $errSchemaPath = $currErrSchemaPath;
131 } else if ($additionalIsSchema) {
132 if ($removeAdditional == 'failing') {
133 out += ' var ' + ($errs) + ' = errors; ';
134 var $wasComposite = it.compositeRule;
135 it.compositeRule = $it.compositeRule = true;
136 $it.schema = $aProperties;
137 $it.schemaPath = it.schemaPath + '.additionalProperties';
138 $it.errSchemaPath = it.errSchemaPath + '/additionalProperties';
139 $it.errorPath = it.opts._errorDataPathProperty ? it.errorPath : it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
140 var $passData = $data + '[' + $key + ']';
141 $it.dataPathArr[$dataNxt] = $key;
142 var $code = it.validate($it);
143 $it.baseId = $currentBaseId;
144 if (it.util.varOccurences($code, $nextData) < 2) {
145 out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
147 out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
149 out += ' if (!' + ($nextValid) + ') { errors = ' + ($errs) + '; if (validate.errors !== null) { if (errors) validate.errors.length = errors; else validate.errors = null; } delete ' + ($data) + '[' + ($key) + ']; } ';
150 it.compositeRule = $it.compositeRule = $wasComposite;
152 $it.schema = $aProperties;
153 $it.schemaPath = it.schemaPath + '.additionalProperties';
154 $it.errSchemaPath = it.errSchemaPath + '/additionalProperties';
155 $it.errorPath = it.opts._errorDataPathProperty ? it.errorPath : it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
156 var $passData = $data + '[' + $key + ']';
157 $it.dataPathArr[$dataNxt] = $key;
158 var $code = it.validate($it);
159 $it.baseId = $currentBaseId;
160 if (it.util.varOccurences($code, $nextData) < 2) {
161 out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
163 out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
166 out += ' if (!' + ($nextValid) + ') break; ';
170 it.errorPath = $currentErrorPath;
172 if ($someProperties) {
177 out += ' if (' + ($nextValid) + ') { ';
178 $closingBraces += '}';
181 var $useDefaults = it.opts.useDefaults && !it.compositeRule;
182 if ($schemaKeys.length) {
183 var arr4 = $schemaKeys;
185 var $propertyKey, i4 = -1,
186 l4 = arr4.length - 1;
188 $propertyKey = arr4[i4 += 1];
189 var $sch = $schema[$propertyKey];
190 if (it.util.schemaHasRules($sch, it.RULES.all)) {
191 var $prop = it.util.getProperty($propertyKey),
192 $passData = $data + $prop,
193 $hasDefault = $useDefaults && $sch.default !== undefined;
195 $it.schemaPath = $schemaPath + $prop;
196 $it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($propertyKey);
197 $it.errorPath = it.util.getPath(it.errorPath, $propertyKey, it.opts.jsonPointers);
198 $it.dataPathArr[$dataNxt] = it.util.toQuotedString($propertyKey);
199 var $code = it.validate($it);
200 $it.baseId = $currentBaseId;
201 if (it.util.varOccurences($code, $nextData) < 2) {
202 $code = it.util.varReplace($code, $nextData, $passData);
203 var $useData = $passData;
205 var $useData = $nextData;
206 out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ';
209 out += ' ' + ($code) + ' ';
211 if ($requiredHash && $requiredHash[$propertyKey]) {
212 out += ' if (' + ($useData) + ' === undefined) { ' + ($nextValid) + ' = false; ';
213 var $currentErrorPath = it.errorPath,
214 $currErrSchemaPath = $errSchemaPath,
215 $missingProperty = it.util.escapeQuotes($propertyKey);
216 if (it.opts._errorDataPathProperty) {
217 it.errorPath = it.util.getPath($currentErrorPath, $propertyKey, it.opts.jsonPointers);
219 $errSchemaPath = it.errSchemaPath + '/required';
220 var $$outStack = $$outStack || [];
221 $$outStack.push(out);
222 out = ''; /* istanbul ignore else */
223 if (it.createErrors !== false) {
224 out += ' { keyword: \'' + ($errorKeyword || 'required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
225 if (it.opts.messages !== false) {
226 out += ' , message: \'';
227 if (it.opts._errorDataPathProperty) {
228 out += 'is a required property';
230 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
234 if (it.opts.verbose) {
235 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
242 out = $$outStack.pop();
243 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
245 out += ' throw new ValidationError([' + (__err) + ']); ';
247 out += ' validate.errors = [' + (__err) + ']; return false; ';
250 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
252 $errSchemaPath = $currErrSchemaPath;
253 it.errorPath = $currentErrorPath;
257 out += ' if (' + ($useData) + ' === undefined) { ' + ($nextValid) + ' = true; } else { ';
259 out += ' if (' + ($useData) + ' !== undefined) { ';
262 out += ' ' + ($code) + ' } ';
266 out += ' if (' + ($nextValid) + ') { ';
267 $closingBraces += '}';
272 var arr5 = $pPropertyKeys;
274 var $pProperty, i5 = -1,
275 l5 = arr5.length - 1;
277 $pProperty = arr5[i5 += 1];
278 var $sch = $pProperties[$pProperty];
279 if (it.util.schemaHasRules($sch, it.RULES.all)) {
281 $it.schemaPath = it.schemaPath + '.patternProperties' + it.util.getProperty($pProperty);
282 $it.errSchemaPath = it.errSchemaPath + '/patternProperties/' + it.util.escapeFragment($pProperty);
283 out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
284 if ($ownProperties) {
285 out += ' if (!Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($key) + ')) continue; ';
287 out += ' if (' + (it.usePattern($pProperty)) + '.test(' + ($key) + ')) { ';
288 $it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
289 var $passData = $data + '[' + $key + ']';
290 $it.dataPathArr[$dataNxt] = $key;
291 var $code = it.validate($it);
292 $it.baseId = $currentBaseId;
293 if (it.util.varOccurences($code, $nextData) < 2) {
294 out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
296 out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
299 out += ' if (!' + ($nextValid) + ') break; ';
303 out += ' else ' + ($nextValid) + ' = true; ';
307 out += ' if (' + ($nextValid) + ') { ';
308 $closingBraces += '}';
314 var arr6 = $pgPropertyKeys;
316 var $pgProperty, i6 = -1,
317 l6 = arr6.length - 1;
319 $pgProperty = arr6[i6 += 1];
320 var $pgSchema = $pgProperties[$pgProperty],
321 $sch = $pgSchema.schema;
322 if (it.util.schemaHasRules($sch, it.RULES.all)) {
324 $it.schemaPath = it.schemaPath + '.patternGroups' + it.util.getProperty($pgProperty) + '.schema';
325 $it.errSchemaPath = it.errSchemaPath + '/patternGroups/' + it.util.escapeFragment($pgProperty) + '/schema';
326 out += ' var pgPropCount' + ($lvl) + ' = 0; for (var ' + ($key) + ' in ' + ($data) + ') { ';
327 if ($ownProperties) {
328 out += ' if (!Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($key) + ')) continue; ';
330 out += ' if (' + (it.usePattern($pgProperty)) + '.test(' + ($key) + ')) { pgPropCount' + ($lvl) + '++; ';
331 $it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
332 var $passData = $data + '[' + $key + ']';
333 $it.dataPathArr[$dataNxt] = $key;
334 var $code = it.validate($it);
335 $it.baseId = $currentBaseId;
336 if (it.util.varOccurences($code, $nextData) < 2) {
337 out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
339 out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
342 out += ' if (!' + ($nextValid) + ') break; ';
346 out += ' else ' + ($nextValid) + ' = true; ';
350 out += ' if (' + ($nextValid) + ') { ';
351 $closingBraces += '}';
353 var $pgMin = $pgSchema.minimum,
354 $pgMax = $pgSchema.maximum;
355 if ($pgMin !== undefined || $pgMax !== undefined) {
356 out += ' var ' + ($valid) + ' = true; ';
357 var $currErrSchemaPath = $errSchemaPath;
358 if ($pgMin !== undefined) {
361 $moreOrLess = 'less';
362 out += ' ' + ($valid) + ' = pgPropCount' + ($lvl) + ' >= ' + ($pgMin) + '; ';
363 $errSchemaPath = it.errSchemaPath + '/patternGroups/minimum';
364 out += ' if (!' + ($valid) + ') { ';
365 var $$outStack = $$outStack || [];
366 $$outStack.push(out);
367 out = ''; /* istanbul ignore else */
368 if (it.createErrors !== false) {
369 out += ' { keyword: \'' + ($errorKeyword || 'patternGroups') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { reason: \'' + ($reason) + '\', limit: ' + ($limit) + ', pattern: \'' + (it.util.escapeQuotes($pgProperty)) + '\' } ';
370 if (it.opts.messages !== false) {
371 out += ' , message: \'should NOT have ' + ($moreOrLess) + ' than ' + ($limit) + ' properties matching pattern "' + (it.util.escapeQuotes($pgProperty)) + '"\' ';
373 if (it.opts.verbose) {
374 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
381 out = $$outStack.pop();
382 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
384 out += ' throw new ValidationError([' + (__err) + ']); ';
386 out += ' validate.errors = [' + (__err) + ']; return false; ';
389 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
392 if ($pgMax !== undefined) {
396 if ($pgMax !== undefined) {
399 $moreOrLess = 'more';
400 out += ' ' + ($valid) + ' = pgPropCount' + ($lvl) + ' <= ' + ($pgMax) + '; ';
401 $errSchemaPath = it.errSchemaPath + '/patternGroups/maximum';
402 out += ' if (!' + ($valid) + ') { ';
403 var $$outStack = $$outStack || [];
404 $$outStack.push(out);
405 out = ''; /* istanbul ignore else */
406 if (it.createErrors !== false) {
407 out += ' { keyword: \'' + ($errorKeyword || 'patternGroups') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { reason: \'' + ($reason) + '\', limit: ' + ($limit) + ', pattern: \'' + (it.util.escapeQuotes($pgProperty)) + '\' } ';
408 if (it.opts.messages !== false) {
409 out += ' , message: \'should NOT have ' + ($moreOrLess) + ' than ' + ($limit) + ' properties matching pattern "' + (it.util.escapeQuotes($pgProperty)) + '"\' ';
411 if (it.opts.verbose) {
412 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
419 out = $$outStack.pop();
420 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
422 out += ' throw new ValidationError([' + (__err) + ']); ';
424 out += ' validate.errors = [' + (__err) + ']; return false; ';
427 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
431 $errSchemaPath = $currErrSchemaPath;
433 out += ' if (' + ($valid) + ') { ';
434 $closingBraces += '}';
442 out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
444 out = it.util.cleanUpCode(out);