2 module.exports = function generate_items(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 $idx = 'i' + $lvl,
19 $dataNxt = $it.dataLevel = it.dataLevel + 1,
20 $nextData = 'data' + $dataNxt,
21 $currentBaseId = it.baseId;
22 out += 'var ' + ($errs) + ' = errors;var ' + ($valid) + ';';
23 if (Array.isArray($schema)) {
24 var $additionalItems = it.schema.additionalItems;
25 if ($additionalItems === false) {
26 out += ' ' + ($valid) + ' = ' + ($data) + '.length <= ' + ($schema.length) + '; ';
27 var $currErrSchemaPath = $errSchemaPath;
28 $errSchemaPath = it.errSchemaPath + '/additionalItems';
29 out += ' if (!' + ($valid) + ') { ';
30 var $$outStack = $$outStack || [];
32 out = ''; /* istanbul ignore else */
33 if (it.createErrors !== false) {
34 out += ' { keyword: \'' + ($errorKeyword || 'additionalItems') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { limit: ' + ($schema.length) + ' } ';
35 if (it.opts.messages !== false) {
36 out += ' , message: \'should NOT have more than ' + ($schema.length) + ' items\' ';
38 if (it.opts.verbose) {
39 out += ' , schema: false , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
46 out = $$outStack.pop();
47 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
49 out += ' throw new ValidationError([' + (__err) + ']); ';
51 out += ' validate.errors = [' + (__err) + ']; return false; ';
54 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
57 $errSchemaPath = $currErrSchemaPath;
59 $closingBraces += '}';
69 if (it.util.schemaHasRules($sch, it.RULES.all)) {
70 out += ' ' + ($nextValid) + ' = true; if (' + ($data) + '.length > ' + ($i) + ') { ';
71 var $passData = $data + '[' + $i + ']';
73 $it.schemaPath = $schemaPath + '[' + $i + ']';
74 $it.errSchemaPath = $errSchemaPath + '/' + $i;
75 $it.errorPath = it.util.getPathExpr(it.errorPath, $i, it.opts.jsonPointers, true);
76 $it.dataPathArr[$dataNxt] = $i;
77 var $code = it.validate($it);
78 $it.baseId = $currentBaseId;
79 if (it.util.varOccurences($code, $nextData) < 2) {
80 out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
82 out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
86 out += ' if (' + ($nextValid) + ') { ';
87 $closingBraces += '}';
92 if (typeof $additionalItems == 'object' && it.util.schemaHasRules($additionalItems, it.RULES.all)) {
93 $it.schema = $additionalItems;
94 $it.schemaPath = it.schemaPath + '.additionalItems';
95 $it.errSchemaPath = it.errSchemaPath + '/additionalItems';
96 out += ' ' + ($nextValid) + ' = true; if (' + ($data) + '.length > ' + ($schema.length) + ') { for (var ' + ($idx) + ' = ' + ($schema.length) + '; ' + ($idx) + ' < ' + ($data) + '.length; ' + ($idx) + '++) { ';
97 $it.errorPath = it.util.getPathExpr(it.errorPath, $idx, it.opts.jsonPointers, true);
98 var $passData = $data + '[' + $idx + ']';
99 $it.dataPathArr[$dataNxt] = $idx;
100 var $code = it.validate($it);
101 $it.baseId = $currentBaseId;
102 if (it.util.varOccurences($code, $nextData) < 2) {
103 out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
105 out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
108 out += ' if (!' + ($nextValid) + ') break; ';
112 out += ' if (' + ($nextValid) + ') { ';
113 $closingBraces += '}';
116 } else if (it.util.schemaHasRules($schema, it.RULES.all)) {
117 $it.schema = $schema;
118 $it.schemaPath = $schemaPath;
119 $it.errSchemaPath = $errSchemaPath;
120 out += ' for (var ' + ($idx) + ' = ' + (0) + '; ' + ($idx) + ' < ' + ($data) + '.length; ' + ($idx) + '++) { ';
121 $it.errorPath = it.util.getPathExpr(it.errorPath, $idx, it.opts.jsonPointers, true);
122 var $passData = $data + '[' + $idx + ']';
123 $it.dataPathArr[$dataNxt] = $idx;
124 var $code = it.validate($it);
125 $it.baseId = $currentBaseId;
126 if (it.util.varOccurences($code, $nextData) < 2) {
127 out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
129 out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
132 out += ' if (!' + ($nextValid) + ') break; ';
136 out += ' if (' + ($nextValid) + ') { ';
137 $closingBraces += '}';
141 out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
143 out = it.util.cleanUpCode(out);