2 module.exports = function generate_required(it, $keyword, $ruleType) {
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,
15 out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
16 $schemaValue = 'schema' + $lvl;
18 $schemaValue = $schema;
20 var $vSchema = 'schema' + $lvl;
22 if ($schema.length < it.opts.loopRequired && it.schema.properties && Object.keys(it.schema.properties).length) {
26 var $property, i1 = -1,
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;
37 var $required = $schema;
40 if ($isData || $required.length) {
41 var $currentErrorPath = it.errorPath,
42 $loopRequired = $isData || $required.length >= it.opts.loopRequired,
43 $ownProperties = it.opts.ownProperties;
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 ';
62 out += ' && Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($vSchema) + '[' + ($i) + ']) ';
64 out += '; if (!' + ($valid) + ') break; } ';
68 out += ' if (!' + ($valid) + ') { ';
69 var $$outStack = $$outStack || [];
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';
79 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
83 if (it.opts.verbose) {
84 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
91 out = $$outStack.pop();
92 if (!it.compositeRule && $breakOnError) {
93 /* istanbul ignore if */
95 out += ' throw new ValidationError([' + (__err) + ']); ';
97 out += ' validate.errors = [' + (__err) + ']; return false; ';
100 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
105 var arr2 = $required;
107 var $propertyKey, $i = -1,
108 l2 = arr2.length - 1;
110 $propertyKey = arr2[$i += 1];
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)) + '\') ';
120 out += ') && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString(it.opts.jsonPointers ? $propertyKey : $prop)) + ') ) ';
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;
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';
139 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
143 if (it.opts.verbose) {
144 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
151 out = $$outStack.pop();
152 if (!it.compositeRule && $breakOnError) {
153 /* istanbul ignore if */
155 out += ' throw new ValidationError([' + (__err) + ']); ';
157 out += ' validate.errors = [' + (__err) + ']; return false; ';
160 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
167 out += ' var ' + ($vSchema) + ' = validate.schema' + ($schemaPath) + '; ';
170 $propertyPath = 'schema' + $lvl + '[' + $i + ']',
171 $missingProperty = '\' + ' + $propertyPath + ' + \'';
172 if (it.opts._errorDataPathProperty) {
173 it.errorPath = it.util.getPathExpr($currentErrorPath, $propertyPath, it.opts.jsonPointers);
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';
184 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
188 if (it.opts.verbose) {
189 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
195 out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } else if (' + ($vSchema) + ' !== undefined) { ';
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) + ']) ';
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';
209 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
213 if (it.opts.verbose) {
214 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
220 out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } } ';
225 var arr3 = $required;
227 var $propertyKey, i3 = -1,
228 l3 = arr3.length - 1;
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);
237 out += ' if ( ' + ($useData) + ' === undefined ';
238 if ($ownProperties) {
239 out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
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';
249 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
253 if (it.opts.verbose) {
254 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
260 out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } ';
265 it.errorPath = $currentErrorPath;
266 } else if ($breakOnError) {
267 out += ' if (true) {';