2 module.exports = function generate_properties(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 $errs = 'errs__' + $lvl;
12 var $it = it.util.copy(it);
13 var $closingBraces = '';
15 var $nextValid = 'valid' + $it.level;
16 var $key = 'key' + $lvl,
18 $dataNxt = $it.dataLevel = it.dataLevel + 1,
19 $nextData = 'data' + $dataNxt,
20 $dataProperties = 'dataProperties' + $lvl;
21 var $schemaKeys = Object.keys($schema || {}).filter(notProto),
22 $pProperties = it.schema.patternProperties || {},
23 $pPropertyKeys = Object.keys($pProperties).filter(notProto),
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.$data && $required.$data) && $required.length < it.opts.loopRequired) {
34 var $requiredHash = it.util.toHash($required);
37 function notProto(p) {
38 return p !== '__proto__';
40 out += 'var ' + ($errs) + ' = errors;var ' + ($nextValid) + ' = true;';
42 out += ' var ' + ($dataProperties) + ' = undefined;';
44 if ($checkAdditional) {
46 out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; ';
48 out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
50 if ($someProperties) {
51 out += ' var isAdditional' + ($lvl) + ' = !(false ';
52 if ($schemaKeys.length) {
53 if ($schemaKeys.length > 8) {
54 out += ' || validate.schema' + ($schemaPath) + '.hasOwnProperty(' + ($key) + ') ';
56 var arr1 = $schemaKeys;
58 var $propertyKey, i1 = -1,
61 $propertyKey = arr1[i1 += 1];
62 out += ' || ' + ($key) + ' == ' + (it.util.toQuotedString($propertyKey)) + ' ';
67 if ($pPropertyKeys.length) {
68 var arr2 = $pPropertyKeys;
70 var $pProperty, $i = -1,
73 $pProperty = arr2[$i += 1];
74 out += ' || ' + (it.usePattern($pProperty)) + '.test(' + ($key) + ') ';
78 out += ' ); if (isAdditional' + ($lvl) + ') { ';
80 if ($removeAdditional == 'all') {
81 out += ' delete ' + ($data) + '[' + ($key) + ']; ';
83 var $currentErrorPath = it.errorPath;
84 var $additionalProperty = '\' + ' + $key + ' + \'';
85 if (it.opts._errorDataPathProperty) {
86 it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
89 if ($removeAdditional) {
90 out += ' delete ' + ($data) + '[' + ($key) + ']; ';
92 out += ' ' + ($nextValid) + ' = false; ';
93 var $currErrSchemaPath = $errSchemaPath;
94 $errSchemaPath = it.errSchemaPath + '/additionalProperties';
95 var $$outStack = $$outStack || [];
97 out = ''; /* istanbul ignore else */
98 if (it.createErrors !== false) {
99 out += ' { keyword: \'' + ('additionalProperties') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { additionalProperty: \'' + ($additionalProperty) + '\' } ';
100 if (it.opts.messages !== false) {
101 out += ' , message: \'';
102 if (it.opts._errorDataPathProperty) {
103 out += 'is an invalid additional property';
105 out += 'should NOT have additional properties';
109 if (it.opts.verbose) {
110 out += ' , schema: false , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
117 out = $$outStack.pop();
118 if (!it.compositeRule && $breakOnError) {
119 /* istanbul ignore if */
121 out += ' throw new ValidationError([' + (__err) + ']); ';
123 out += ' validate.errors = [' + (__err) + ']; return false; ';
126 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
128 $errSchemaPath = $currErrSchemaPath;
133 } else if ($additionalIsSchema) {
134 if ($removeAdditional == 'failing') {
135 out += ' var ' + ($errs) + ' = errors; ';
136 var $wasComposite = it.compositeRule;
137 it.compositeRule = $it.compositeRule = true;
138 $it.schema = $aProperties;
139 $it.schemaPath = it.schemaPath + '.additionalProperties';
140 $it.errSchemaPath = it.errSchemaPath + '/additionalProperties';
141 $it.errorPath = it.opts._errorDataPathProperty ? it.errorPath : it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
142 var $passData = $data + '[' + $key + ']';
143 $it.dataPathArr[$dataNxt] = $key;
144 var $code = it.validate($it);
145 $it.baseId = $currentBaseId;
146 if (it.util.varOccurences($code, $nextData) < 2) {
147 out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
149 out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
151 out += ' if (!' + ($nextValid) + ') { errors = ' + ($errs) + '; if (validate.errors !== null) { if (errors) validate.errors.length = errors; else validate.errors = null; } delete ' + ($data) + '[' + ($key) + ']; } ';
152 it.compositeRule = $it.compositeRule = $wasComposite;
154 $it.schema = $aProperties;
155 $it.schemaPath = it.schemaPath + '.additionalProperties';
156 $it.errSchemaPath = it.errSchemaPath + '/additionalProperties';
157 $it.errorPath = it.opts._errorDataPathProperty ? it.errorPath : it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
158 var $passData = $data + '[' + $key + ']';
159 $it.dataPathArr[$dataNxt] = $key;
160 var $code = it.validate($it);
161 $it.baseId = $currentBaseId;
162 if (it.util.varOccurences($code, $nextData) < 2) {
163 out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
165 out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
168 out += ' if (!' + ($nextValid) + ') break; ';
172 it.errorPath = $currentErrorPath;
174 if ($someProperties) {
179 out += ' if (' + ($nextValid) + ') { ';
180 $closingBraces += '}';
183 var $useDefaults = it.opts.useDefaults && !it.compositeRule;
184 if ($schemaKeys.length) {
185 var arr3 = $schemaKeys;
187 var $propertyKey, i3 = -1,
188 l3 = arr3.length - 1;
190 $propertyKey = arr3[i3 += 1];
191 var $sch = $schema[$propertyKey];
192 if ((it.opts.strictKeywords ? typeof $sch == 'object' && Object.keys($sch).length > 0 : it.util.schemaHasRules($sch, it.RULES.all))) {
193 var $prop = it.util.getProperty($propertyKey),
194 $passData = $data + $prop,
195 $hasDefault = $useDefaults && $sch.default !== undefined;
197 $it.schemaPath = $schemaPath + $prop;
198 $it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($propertyKey);
199 $it.errorPath = it.util.getPath(it.errorPath, $propertyKey, it.opts.jsonPointers);
200 $it.dataPathArr[$dataNxt] = it.util.toQuotedString($propertyKey);
201 var $code = it.validate($it);
202 $it.baseId = $currentBaseId;
203 if (it.util.varOccurences($code, $nextData) < 2) {
204 $code = it.util.varReplace($code, $nextData, $passData);
205 var $useData = $passData;
207 var $useData = $nextData;
208 out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ';
211 out += ' ' + ($code) + ' ';
213 if ($requiredHash && $requiredHash[$propertyKey]) {
214 out += ' if ( ' + ($useData) + ' === undefined ';
215 if ($ownProperties) {
216 out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
218 out += ') { ' + ($nextValid) + ' = false; ';
219 var $currentErrorPath = it.errorPath,
220 $currErrSchemaPath = $errSchemaPath,
221 $missingProperty = it.util.escapeQuotes($propertyKey);
222 if (it.opts._errorDataPathProperty) {
223 it.errorPath = it.util.getPath($currentErrorPath, $propertyKey, it.opts.jsonPointers);
225 $errSchemaPath = it.errSchemaPath + '/required';
226 var $$outStack = $$outStack || [];
227 $$outStack.push(out);
228 out = ''; /* istanbul ignore else */
229 if (it.createErrors !== false) {
230 out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
231 if (it.opts.messages !== false) {
232 out += ' , message: \'';
233 if (it.opts._errorDataPathProperty) {
234 out += 'is a required property';
236 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
240 if (it.opts.verbose) {
241 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
248 out = $$outStack.pop();
249 if (!it.compositeRule && $breakOnError) {
250 /* istanbul ignore if */
252 out += ' throw new ValidationError([' + (__err) + ']); ';
254 out += ' validate.errors = [' + (__err) + ']; return false; ';
257 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
259 $errSchemaPath = $currErrSchemaPath;
260 it.errorPath = $currentErrorPath;
264 out += ' if ( ' + ($useData) + ' === undefined ';
265 if ($ownProperties) {
266 out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
268 out += ') { ' + ($nextValid) + ' = true; } else { ';
270 out += ' if (' + ($useData) + ' !== undefined ';
271 if ($ownProperties) {
272 out += ' && Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
277 out += ' ' + ($code) + ' } ';
281 out += ' if (' + ($nextValid) + ') { ';
282 $closingBraces += '}';
287 if ($pPropertyKeys.length) {
288 var arr4 = $pPropertyKeys;
290 var $pProperty, i4 = -1,
291 l4 = arr4.length - 1;
293 $pProperty = arr4[i4 += 1];
294 var $sch = $pProperties[$pProperty];
295 if ((it.opts.strictKeywords ? typeof $sch == 'object' && Object.keys($sch).length > 0 : it.util.schemaHasRules($sch, it.RULES.all))) {
297 $it.schemaPath = it.schemaPath + '.patternProperties' + it.util.getProperty($pProperty);
298 $it.errSchemaPath = it.errSchemaPath + '/patternProperties/' + it.util.escapeFragment($pProperty);
299 if ($ownProperties) {
300 out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; ';
302 out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
304 out += ' if (' + (it.usePattern($pProperty)) + '.test(' + ($key) + ')) { ';
305 $it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
306 var $passData = $data + '[' + $key + ']';
307 $it.dataPathArr[$dataNxt] = $key;
308 var $code = it.validate($it);
309 $it.baseId = $currentBaseId;
310 if (it.util.varOccurences($code, $nextData) < 2) {
311 out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
313 out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
316 out += ' if (!' + ($nextValid) + ') break; ';
320 out += ' else ' + ($nextValid) + ' = true; ';
324 out += ' if (' + ($nextValid) + ') { ';
325 $closingBraces += '}';
332 out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';