2 module.exports = function generate_required(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 $isData = it.opts.v5 && $schema && $schema.$data,
16 out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
17 $schemaValue = 'schema' + $lvl;
19 $schemaValue = $schema;
21 var $vSchema = 'schema' + $lvl;
23 if ($schema.length < it.opts.loopRequired && it.schema.properties && Object.keys(it.schema.properties).length) {
27 var $property, i1 = -1,
30 $property = arr1[i1 += 1];
31 var $propertySch = it.schema.properties[$property];
32 if (!($propertySch && it.util.schemaHasRules($propertySch, it.RULES.all))) {
33 $required[$required.length] = $property;
38 var $required = $schema;
41 if ($isData || $required.length) {
42 var $currentErrorPath = it.errorPath,
43 $loopRequired = $isData || $required.length >= it.opts.loopRequired;
45 out += ' var missing' + ($lvl) + '; ';
48 out += ' var ' + ($vSchema) + ' = validate.schema' + ($schemaPath) + '; ';
51 $propertyPath = 'schema' + $lvl + '[' + $i + ']',
52 $missingProperty = '\' + ' + $propertyPath + ' + \'';
53 if (it.opts._errorDataPathProperty) {
54 it.errorPath = it.util.getPathExpr($currentErrorPath, $propertyPath, it.opts.jsonPointers);
56 out += ' var ' + ($valid) + ' = true; ';
58 out += ' if (schema' + ($lvl) + ' === undefined) ' + ($valid) + ' = true; else if (!Array.isArray(schema' + ($lvl) + ')) ' + ($valid) + ' = false; else {';
60 out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < ' + ($vSchema) + '.length; ' + ($i) + '++) { ' + ($valid) + ' = ' + ($data) + '[' + ($vSchema) + '[' + ($i) + ']] !== undefined; if (!' + ($valid) + ') break; } ';
64 out += ' if (!' + ($valid) + ') { ';
65 var $$outStack = $$outStack || [];
67 out = ''; /* istanbul ignore else */
68 if (it.createErrors !== false) {
69 out += ' { keyword: \'' + ($errorKeyword || 'required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
70 if (it.opts.messages !== false) {
71 out += ' , message: \'';
72 if (it.opts._errorDataPathProperty) {
73 out += 'is a required property';
75 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
79 if (it.opts.verbose) {
80 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
87 out = $$outStack.pop();
88 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
90 out += ' throw new ValidationError([' + (__err) + ']); ';
92 out += ' validate.errors = [' + (__err) + ']; return false; ';
95 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
100 var arr2 = $required;
102 var _$property, $i = -1,
103 l2 = arr2.length - 1;
105 _$property = arr2[$i += 1];
109 var $prop = it.util.getProperty(_$property);
110 out += ' ( ' + ($data) + ($prop) + ' === undefined && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString(it.opts.jsonPointers ? _$property : $prop)) + ') ) ';
114 var $propertyPath = 'missing' + $lvl,
115 $missingProperty = '\' + ' + $propertyPath + ' + \'';
116 if (it.opts._errorDataPathProperty) {
117 it.errorPath = it.opts.jsonPointers ? it.util.getPathExpr($currentErrorPath, $propertyPath, true) : $currentErrorPath + ' + ' + $propertyPath;
119 var $$outStack = $$outStack || [];
120 $$outStack.push(out);
121 out = ''; /* istanbul ignore else */
122 if (it.createErrors !== false) {
123 out += ' { keyword: \'' + ($errorKeyword || 'required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
124 if (it.opts.messages !== false) {
125 out += ' , message: \'';
126 if (it.opts._errorDataPathProperty) {
127 out += 'is a required property';
129 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
133 if (it.opts.verbose) {
134 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
141 out = $$outStack.pop();
142 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
144 out += ' throw new ValidationError([' + (__err) + ']); ';
146 out += ' validate.errors = [' + (__err) + ']; return false; ';
149 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
156 out += ' var ' + ($vSchema) + ' = validate.schema' + ($schemaPath) + '; ';
159 $propertyPath = 'schema' + $lvl + '[' + $i + ']',
160 $missingProperty = '\' + ' + $propertyPath + ' + \'';
161 if (it.opts._errorDataPathProperty) {
162 it.errorPath = it.util.getPathExpr($currentErrorPath, $propertyPath, it.opts.jsonPointers);
165 out += ' if (' + ($vSchema) + ' && !Array.isArray(' + ($vSchema) + ')) { var err = '; /* istanbul ignore else */
166 if (it.createErrors !== false) {
167 out += ' { keyword: \'' + ($errorKeyword || 'required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
168 if (it.opts.messages !== false) {
169 out += ' , message: \'';
170 if (it.opts._errorDataPathProperty) {
171 out += 'is a required property';
173 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
177 if (it.opts.verbose) {
178 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
184 out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } else if (' + ($vSchema) + ' !== undefined) { ';
186 out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < ' + ($vSchema) + '.length; ' + ($i) + '++) { if (' + ($data) + '[' + ($vSchema) + '[' + ($i) + ']] === undefined) { var err = '; /* istanbul ignore else */
187 if (it.createErrors !== false) {
188 out += ' { keyword: \'' + ($errorKeyword || 'required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
189 if (it.opts.messages !== false) {
190 out += ' , message: \'';
191 if (it.opts._errorDataPathProperty) {
192 out += 'is a required property';
194 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
198 if (it.opts.verbose) {
199 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
205 out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } } ';
210 var arr3 = $required;
212 var $reqProperty, i3 = -1,
213 l3 = arr3.length - 1;
215 $reqProperty = arr3[i3 += 1];
216 var $prop = it.util.getProperty($reqProperty),
217 $missingProperty = it.util.escapeQuotes($reqProperty);
218 if (it.opts._errorDataPathProperty) {
219 it.errorPath = it.util.getPath($currentErrorPath, $reqProperty, it.opts.jsonPointers);
221 out += ' if (' + ($data) + ($prop) + ' === undefined) { var err = '; /* istanbul ignore else */
222 if (it.createErrors !== false) {
223 out += ' { keyword: \'' + ($errorKeyword || 'required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
224 if (it.opts.messages !== false) {
225 out += ' , message: \'';
226 if (it.opts._errorDataPathProperty) {
227 out += 'is a required property';
229 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
233 if (it.opts.verbose) {
234 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
240 out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } ';
245 it.errorPath = $currentErrorPath;
246 } else if ($breakOnError) {
247 out += ' if (true) {';