1
|
'use strict';
|
2
|
module.exports = function generate_properties(it, $keyword, $ruleType) {
|
3
|
var out = ' ';
|
4
|
var $lvl = it.level;
|
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 = '';
|
14
|
$it.level++;
|
15
|
var $nextValid = 'valid' + $it.level;
|
16
|
var $key = 'key' + $lvl,
|
17
|
$idx = 'idx' + $lvl,
|
18
|
$dataNxt = $it.dataLevel = it.dataLevel + 1,
|
19
|
$nextData = 'data' + $dataNxt,
|
20
|
$dataProperties = 'dataProperties' + $lvl;
|
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.$data && $required.$data) && $required.length < it.opts.loopRequired) var $requiredHash = it.util.toHash($required);
|
34
|
out += 'var ' + ($errs) + ' = errors;var ' + ($nextValid) + ' = true;';
|
35
|
if ($ownProperties) {
|
36
|
out += ' var ' + ($dataProperties) + ' = undefined;';
|
37
|
}
|
38
|
if ($checkAdditional) {
|
39
|
if ($ownProperties) {
|
40
|
out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; ';
|
41
|
} else {
|
42
|
out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
|
43
|
}
|
44
|
if ($someProperties) {
|
45
|
out += ' var isAdditional' + ($lvl) + ' = !(false ';
|
46
|
if ($schemaKeys.length) {
|
47
|
if ($schemaKeys.length > 8) {
|
48
|
out += ' || validate.schema' + ($schemaPath) + '.hasOwnProperty(' + ($key) + ') ';
|
49
|
} else {
|
50
|
var arr1 = $schemaKeys;
|
51
|
if (arr1) {
|
52
|
var $propertyKey, i1 = -1,
|
53
|
l1 = arr1.length - 1;
|
54
|
while (i1 < l1) {
|
55
|
$propertyKey = arr1[i1 += 1];
|
56
|
out += ' || ' + ($key) + ' == ' + (it.util.toQuotedString($propertyKey)) + ' ';
|
57
|
}
|
58
|
}
|
59
|
}
|
60
|
}
|
61
|
if ($pPropertyKeys.length) {
|
62
|
var arr2 = $pPropertyKeys;
|
63
|
if (arr2) {
|
64
|
var $pProperty, $i = -1,
|
65
|
l2 = arr2.length - 1;
|
66
|
while ($i < l2) {
|
67
|
$pProperty = arr2[$i += 1];
|
68
|
out += ' || ' + (it.usePattern($pProperty)) + '.test(' + ($key) + ') ';
|
69
|
}
|
70
|
}
|
71
|
}
|
72
|
out += ' ); if (isAdditional' + ($lvl) + ') { ';
|
73
|
}
|
74
|
if ($removeAdditional == 'all') {
|
75
|
out += ' delete ' + ($data) + '[' + ($key) + ']; ';
|
76
|
} else {
|
77
|
var $currentErrorPath = it.errorPath;
|
78
|
var $additionalProperty = '\' + ' + $key + ' + \'';
|
79
|
if (it.opts._errorDataPathProperty) {
|
80
|
it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
|
81
|
}
|
82
|
if ($noAdditional) {
|
83
|
if ($removeAdditional) {
|
84
|
out += ' delete ' + ($data) + '[' + ($key) + ']; ';
|
85
|
} else {
|
86
|
out += ' ' + ($nextValid) + ' = false; ';
|
87
|
var $currErrSchemaPath = $errSchemaPath;
|
88
|
$errSchemaPath = it.errSchemaPath + '/additionalProperties';
|
89
|
var $$outStack = $$outStack || [];
|
90
|
$$outStack.push(out);
|
91
|
out = ''; /* istanbul ignore else */
|
92
|
if (it.createErrors !== false) {
|
93
|
out += ' { keyword: \'' + ('additionalProperties') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { additionalProperty: \'' + ($additionalProperty) + '\' } ';
|
94
|
if (it.opts.messages !== false) {
|
95
|
out += ' , message: \'';
|
96
|
if (it.opts._errorDataPathProperty) {
|
97
|
out += 'is an invalid additional property';
|
98
|
} else {
|
99
|
out += 'should NOT have additional properties';
|
100
|
}
|
101
|
out += '\' ';
|
102
|
}
|
103
|
if (it.opts.verbose) {
|
104
|
out += ' , schema: false , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
|
105
|
}
|
106
|
out += ' } ';
|
107
|
} else {
|
108
|
out += ' {} ';
|
109
|
}
|
110
|
var __err = out;
|
111
|
out = $$outStack.pop();
|
112
|
if (!it.compositeRule && $breakOnError) {
|
113
|
/* istanbul ignore if */
|
114
|
if (it.async) {
|
115
|
out += ' throw new ValidationError([' + (__err) + ']); ';
|
116
|
} else {
|
117
|
out += ' validate.errors = [' + (__err) + ']; return false; ';
|
118
|
}
|
119
|
} else {
|
120
|
out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
|
121
|
}
|
122
|
$errSchemaPath = $currErrSchemaPath;
|
123
|
if ($breakOnError) {
|
124
|
out += ' break; ';
|
125
|
}
|
126
|
}
|
127
|
} else if ($additionalIsSchema) {
|
128
|
if ($removeAdditional == 'failing') {
|
129
|
out += ' var ' + ($errs) + ' = errors; ';
|
130
|
var $wasComposite = it.compositeRule;
|
131
|
it.compositeRule = $it.compositeRule = true;
|
132
|
$it.schema = $aProperties;
|
133
|
$it.schemaPath = it.schemaPath + '.additionalProperties';
|
134
|
$it.errSchemaPath = it.errSchemaPath + '/additionalProperties';
|
135
|
$it.errorPath = it.opts._errorDataPathProperty ? it.errorPath : it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
|
136
|
var $passData = $data + '[' + $key + ']';
|
137
|
$it.dataPathArr[$dataNxt] = $key;
|
138
|
var $code = it.validate($it);
|
139
|
$it.baseId = $currentBaseId;
|
140
|
if (it.util.varOccurences($code, $nextData) < 2) {
|
141
|
out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
|
142
|
} else {
|
143
|
out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
|
144
|
}
|
145
|
out += ' if (!' + ($nextValid) + ') { errors = ' + ($errs) + '; if (validate.errors !== null) { if (errors) validate.errors.length = errors; else validate.errors = null; } delete ' + ($data) + '[' + ($key) + ']; } ';
|
146
|
it.compositeRule = $it.compositeRule = $wasComposite;
|
147
|
} else {
|
148
|
$it.schema = $aProperties;
|
149
|
$it.schemaPath = it.schemaPath + '.additionalProperties';
|
150
|
$it.errSchemaPath = it.errSchemaPath + '/additionalProperties';
|
151
|
$it.errorPath = it.opts._errorDataPathProperty ? it.errorPath : it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
|
152
|
var $passData = $data + '[' + $key + ']';
|
153
|
$it.dataPathArr[$dataNxt] = $key;
|
154
|
var $code = it.validate($it);
|
155
|
$it.baseId = $currentBaseId;
|
156
|
if (it.util.varOccurences($code, $nextData) < 2) {
|
157
|
out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
|
158
|
} else {
|
159
|
out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
|
160
|
}
|
161
|
if ($breakOnError) {
|
162
|
out += ' if (!' + ($nextValid) + ') break; ';
|
163
|
}
|
164
|
}
|
165
|
}
|
166
|
it.errorPath = $currentErrorPath;
|
167
|
}
|
168
|
if ($someProperties) {
|
169
|
out += ' } ';
|
170
|
}
|
171
|
out += ' } ';
|
172
|
if ($breakOnError) {
|
173
|
out += ' if (' + ($nextValid) + ') { ';
|
174
|
$closingBraces += '}';
|
175
|
}
|
176
|
}
|
177
|
var $useDefaults = it.opts.useDefaults && !it.compositeRule;
|
178
|
if ($schemaKeys.length) {
|
179
|
var arr3 = $schemaKeys;
|
180
|
if (arr3) {
|
181
|
var $propertyKey, i3 = -1,
|
182
|
l3 = arr3.length - 1;
|
183
|
while (i3 < l3) {
|
184
|
$propertyKey = arr3[i3 += 1];
|
185
|
var $sch = $schema[$propertyKey];
|
186
|
if ((it.opts.strictKeywords ? typeof $sch == 'object' && Object.keys($sch).length > 0 : it.util.schemaHasRules($sch, it.RULES.all))) {
|
187
|
var $prop = it.util.getProperty($propertyKey),
|
188
|
$passData = $data + $prop,
|
189
|
$hasDefault = $useDefaults && $sch.default !== undefined;
|
190
|
$it.schema = $sch;
|
191
|
$it.schemaPath = $schemaPath + $prop;
|
192
|
$it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($propertyKey);
|
193
|
$it.errorPath = it.util.getPath(it.errorPath, $propertyKey, it.opts.jsonPointers);
|
194
|
$it.dataPathArr[$dataNxt] = it.util.toQuotedString($propertyKey);
|
195
|
var $code = it.validate($it);
|
196
|
$it.baseId = $currentBaseId;
|
197
|
if (it.util.varOccurences($code, $nextData) < 2) {
|
198
|
$code = it.util.varReplace($code, $nextData, $passData);
|
199
|
var $useData = $passData;
|
200
|
} else {
|
201
|
var $useData = $nextData;
|
202
|
out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ';
|
203
|
}
|
204
|
if ($hasDefault) {
|
205
|
out += ' ' + ($code) + ' ';
|
206
|
} else {
|
207
|
if ($requiredHash && $requiredHash[$propertyKey]) {
|
208
|
out += ' if ( ' + ($useData) + ' === undefined ';
|
209
|
if ($ownProperties) {
|
210
|
out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
|
211
|
}
|
212
|
out += ') { ' + ($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);
|
218
|
}
|
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: \'' + ('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';
|
229
|
} else {
|
230
|
out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
|
231
|
}
|
232
|
out += '\' ';
|
233
|
}
|
234
|
if (it.opts.verbose) {
|
235
|
out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
|
236
|
}
|
237
|
out += ' } ';
|
238
|
} else {
|
239
|
out += ' {} ';
|
240
|
}
|
241
|
var __err = out;
|
242
|
out = $$outStack.pop();
|
243
|
if (!it.compositeRule && $breakOnError) {
|
244
|
/* istanbul ignore if */
|
245
|
if (it.async) {
|
246
|
out += ' throw new ValidationError([' + (__err) + ']); ';
|
247
|
} else {
|
248
|
out += ' validate.errors = [' + (__err) + ']; return false; ';
|
249
|
}
|
250
|
} else {
|
251
|
out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
|
252
|
}
|
253
|
$errSchemaPath = $currErrSchemaPath;
|
254
|
it.errorPath = $currentErrorPath;
|
255
|
out += ' } else { ';
|
256
|
} else {
|
257
|
if ($breakOnError) {
|
258
|
out += ' if ( ' + ($useData) + ' === undefined ';
|
259
|
if ($ownProperties) {
|
260
|
out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
|
261
|
}
|
262
|
out += ') { ' + ($nextValid) + ' = true; } else { ';
|
263
|
} else {
|
264
|
out += ' if (' + ($useData) + ' !== undefined ';
|
265
|
if ($ownProperties) {
|
266
|
out += ' && Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
|
267
|
}
|
268
|
out += ' ) { ';
|
269
|
}
|
270
|
}
|
271
|
out += ' ' + ($code) + ' } ';
|
272
|
}
|
273
|
}
|
274
|
if ($breakOnError) {
|
275
|
out += ' if (' + ($nextValid) + ') { ';
|
276
|
$closingBraces += '}';
|
277
|
}
|
278
|
}
|
279
|
}
|
280
|
}
|
281
|
if ($pPropertyKeys.length) {
|
282
|
var arr4 = $pPropertyKeys;
|
283
|
if (arr4) {
|
284
|
var $pProperty, i4 = -1,
|
285
|
l4 = arr4.length - 1;
|
286
|
while (i4 < l4) {
|
287
|
$pProperty = arr4[i4 += 1];
|
288
|
var $sch = $pProperties[$pProperty];
|
289
|
if ((it.opts.strictKeywords ? typeof $sch == 'object' && Object.keys($sch).length > 0 : it.util.schemaHasRules($sch, it.RULES.all))) {
|
290
|
$it.schema = $sch;
|
291
|
$it.schemaPath = it.schemaPath + '.patternProperties' + it.util.getProperty($pProperty);
|
292
|
$it.errSchemaPath = it.errSchemaPath + '/patternProperties/' + it.util.escapeFragment($pProperty);
|
293
|
if ($ownProperties) {
|
294
|
out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; ';
|
295
|
} else {
|
296
|
out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
|
297
|
}
|
298
|
out += ' if (' + (it.usePattern($pProperty)) + '.test(' + ($key) + ')) { ';
|
299
|
$it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
|
300
|
var $passData = $data + '[' + $key + ']';
|
301
|
$it.dataPathArr[$dataNxt] = $key;
|
302
|
var $code = it.validate($it);
|
303
|
$it.baseId = $currentBaseId;
|
304
|
if (it.util.varOccurences($code, $nextData) < 2) {
|
305
|
out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
|
306
|
} else {
|
307
|
out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
|
308
|
}
|
309
|
if ($breakOnError) {
|
310
|
out += ' if (!' + ($nextValid) + ') break; ';
|
311
|
}
|
312
|
out += ' } ';
|
313
|
if ($breakOnError) {
|
314
|
out += ' else ' + ($nextValid) + ' = true; ';
|
315
|
}
|
316
|
out += ' } ';
|
317
|
if ($breakOnError) {
|
318
|
out += ' if (' + ($nextValid) + ') { ';
|
319
|
$closingBraces += '}';
|
320
|
}
|
321
|
}
|
322
|
}
|
323
|
}
|
324
|
}
|
325
|
if ($breakOnError) {
|
326
|
out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
|
327
|
}
|
328
|
out = it.util.cleanUpCode(out);
|
329
|
return out;
|
330
|
}
|