-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.js
More file actions
484 lines (379 loc) · 15.7 KB
/
Copy pathapp.js
File metadata and controls
484 lines (379 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
var async = require('async'),
traverse = require('traverse'),
printDebug = require('debug')('json-validator'),
validator = require('validator'),
utils = require('gammautils'),
forEachOwnProperty = utils.object.forEachOwnProperty,
deepSet = utils.object.deepSet,
deepMerge = utils.object.deepMerge,
deepDelete = utils.object.deepDelete,
unflatten = utils.object.unflatten,
constants = require('./constants'),
matchers = require('./matchers').matchers,
customTransforms = require('./matchers').customTransforms,
pushMessage = require('./matchers').pushMessage;
validator.extend('specificLengths', function(string) {
var lengths = Array.prototype.slice.call(arguments, 1);
return lengths.indexOf(string.length) > -1;
});
validator.extend('toNull', function(string) {
if(!string) {
return null;
}
return string;
});
validator.extend('toUpperCase', function(string) {
return string.toUpperCase();
});
validator.extend('toLowerCase', function(string) {
return string.toLowerCase();
});
module.exports.NULL = constants.NULL;
module.exports.extend = function(name, fn) {
validator.extend(name, fn);
};
module.exports.resetMessages = function() {
require('./matchers').resetMessages();
}
module.exports.setMessages = function(messages) {
require('./matchers').setMessages(messages);
}
module.exports.setMessage = function(validator, message) {
require('./matchers').validationMessages[validator] = message;
};
module.exports.setCustomTransforms = function (transforms) {
Object.keys(transforms).forEach(function (customTransformName) {
if(require('./matchers').customTransforms[customTransformName]) {
throw new Error('Transform "' + customTransformName + '" already exists')
}
require('./matchers').customTransforms[customTransformName] = transforms[customTransformName]
})
}
module.exports.setCustomValidators = function (validators) {
Object.keys(validators).forEach(function (customValidatorName) {
if(validator[customValidatorName]) {
throw new Error('Validator "' + customValidatorName + '" already exists')
}
validator.extend(customValidatorName, validators[customValidatorName])
})
}
module.exports.validate = function(object, schema, optionals, debug, callback) {
if(typeof object === 'string') {
object = JSON.parse(object);
}
if(typeof optionals === 'undefined') {
optionals = [];
}
if(typeof optionals === 'boolean') {
debug = optionals;
optionals = [];
}
if(typeof optionals === 'function') {
debug = false;
callback = optionals;
optionals = [];
}
if(typeof debug === 'undefined') {
debug = false;
}
if(typeof debug === 'function') {
callback = debug;
debug = false;
}
var choiceGroups = {
requireds: [], // List of choice groups that are required
sets: {} // List of choice groups that were set
}; //should check sets x requireds and if any required was not set than add an error message
return validate(object, schema, '', [], optionals, choiceGroups, [], debug, callback);
};
function validate(object, _schema, path, messages, optionals, choiceGroups, escapePrefixes, debug, callback) {
if(!Array.isArray(_schema)) {
_schema = [_schema];
}
var messageObject = {},
schema = _schema.reduce(function(previous, current) {
return deepMerge(previous, current);
}, {}),
asyncValidations = [],
asyncTransformations = [];
object = traverse(object);
traverse(schema).forEach(function(node) {
if(!Array.isArray(node)){
return;
}
var array = object.get(this.path);
if(typeof array === 'undefined') {
return;
}
this.update(fixArray(node, array, this.path));
});
function fixArray(array, actualArray, path) {
var times = actualArray.length;
if(times === 0) {
return [];
}
var object = array[0],
isFunction = typeof object === 'function';
if(isFunction) {
array = [];
}
for (var i = 0; i < times - (isFunction ? 0 : 1); i++) {
if(isFunction) {
array.push(object(actualArray[i]));
} else {
array.push(object);
}
}
return array;
}
traverse(schema).forEach(function(node) {
var matchersThatShouldNotContinue = [
'enum', 'transform', 'validate'
];
if(this.parent && matchersThatShouldNotContinue.indexOf(this.parent.key) > -1) {
//if parent is enum do not continue to its parameters
return;
}
if(this.parent && validator[this.parent.key]) {
//if parent is a validator method, do not continue to its parameters
return;
}
var shouldContinue = Array.isArray(node) && (this.key === 'enum' || this.key === 'transform' || this.key === 'validate' || validator[this.key]);
if(typeof node === "object" && !shouldContinue) {
return;
}
var objectPath = traverse.clone(this.path);
objectPath.pop();
var objectValue = object.get(objectPath);
if( this.parent &&
'skip' in this.parent.node &&
this.parent.node['skip'] === true ) {
return;
}
var _objectPath = objectPath.join('.'),
shouldEscape = false;
shouldEscape = escapePrefixes.some(function (escapePrefix) {
return _objectPath.indexOf(escapePrefix) === 0;
});
if(shouldEscape) {
return;
}
if( this.parent && //tem um pai e
'required' in this.parent.node && //o pai tem "required" e
this.parent.node['required'] === false && //o required do pai é falso e
(objectValue === null || typeof objectValue === 'undefined')) {//o objeto corrente não foi fornecido
if('default' in this.parent.node) {
// Tem valor default, então continua
} else {
escapePrefixes.push(objectPath.join('.') + '.');
if(debug) {
console.log('Not required and null or undefined');
}
return; //nem continua
}
}
//didn't find, parent is listed as optional and parent also was not found
if(!objectValue && this.parent && this.parent.parent && find(optionals, this.parent.parent.path.join(".")) && !object.get(this.parent.parent.path) ) {
return;
}
var matcherMethod = this.path.pop();
if(matcherMethod.indexOf('prevent') === 0) {
matcherMethod = 'prevent';
}
if(matcherMethod === 'requiredChoiceGroup') {
return;
}
if(matcherMethod === 'choiceGroup') {
var choiceGroupPath = objectPath.slice(0, -1).join('.') || 'root';
if(this.parent && this.parent.node && this.parent.node.requiredChoiceGroup === true) {
choiceGroups.requireds.push(choiceGroupPath + '|' + node);
}
if(choiceGroups.sets[choiceGroupPath] && choiceGroups.sets[choiceGroupPath][node] && typeof objectValue !== 'undefined') {
// O valor do grupo já havia sido informado anteriormente
return pushMessage(messages, matcherMethod, objectValue, objectPath.join('.'), params, messageObject, require('./matchers').validationMessages[matcherMethod]);
}
if(typeof objectValue !== 'undefined') {
if(!choiceGroups.sets[choiceGroupPath]) {
choiceGroups.sets[choiceGroupPath] = {};
}
choiceGroups.sets[choiceGroupPath][node] = true
}
}
if(matchers[matcherMethod] || customTransforms[matcherMethod]) {
if(matcherMethod === 'transform' && Array.isArray(node)) {
//we have multiple functions so we wrap them into a single function
//with underscore's compose
function compose(args) {
var start = args.length - 1;
return function() {
var i = start;
var result = args[start].apply(this, arguments);
while (i--) result = args[i].call(this, result);
return result;
};
};
node = compose(node.reverse());
}
var that = this;
objectClone = deepMerge({}, object.value);
if(this.parent && this.parent.parent && this.parent.parent.parent && Array.isArray(this.parent.parent.parent.node)) {
objectClone = deepMerge({}, object.get(this.parent.parent.path));
}
function preventNext() {
that.parent.node['skip'] = true;
}
var match = (matchers[matcherMethod] || customTransforms[matcherMethod]).call(objectClone, node, objectValue, objectPath.join("."), messages, optionals, messageObject, preventNext);
if(matcherMethod === 'unset' && match === true) {
//unset returns true or false;
// true - remove
// false - do not remove
deepDelete(object.value, objectPath)
}
if(['transform', 'default'].indexOf(matcherMethod) > -1 || customTransforms[matcherMethod]) {
//Aqui eu posso salvar o objectPath junto com o valor transformado,
//e depois só aplicar se não houverem mensagens.
//
//Neste caso as transformações são aplicadas independente de estar valido ou não
//o que pode não ser desejado.
object.set(objectPath, match);
} else if(['asyncValidate'].indexOf(matcherMethod) > -1) {
asyncValidations.push(match);
} else if(['asyncTransform'].indexOf(matcherMethod) > -1) {
asyncTransformations.push(match);
}
} else if(validator[matcherMethod]) {
var params = [objectValue],
shouldInvert = false;
if(Array.isArray(node)) {
params = params.concat(node);
} else if(typeof node === 'boolean') {
shouldInvert = !node;
}
params = params.map(function(param) {
if(typeof param === 'function') {
param = param.apply(deepMerge({}, object.value));
}
return param;
});
var result = validator[matcherMethod].apply(null, params);
if(typeof result === 'boolean' && matcherMethod !== 'toBoolean') {
if(shouldInvert) {
result = !result;
}
if(!result) {
if(require('./matchers').validationMessages[matcherMethod]) {
pushMessage(messages, matcherMethod, objectValue, objectPath.join('.'), params, messageObject, require('./matchers').validationMessages[matcherMethod]);
} else {
pushMessage(messages, matcherMethod + ':validatorjs', objectValue, objectPath.join('.'), params, messageObject);
}
}
} else {
object.set(objectPath, result);
}
} else {
printDebug("Warning: validator '" + matcherMethod + "' was not found. Skipping!");
}
if(debug) {
console.log(this.path.join(".") + "." + matcherMethod + " === " + node + " | " + objectValue + " >>> " + match);
}
});
var choiceGroupMessagesAdded = {};
choiceGroups.requireds.forEach(function (requiredGroup) {
var _requiredGroup = requiredGroup.split('|'),
groupPath = _requiredGroup[0],
groupId = _requiredGroup[1];
if(!choiceGroups.sets[groupPath] || !choiceGroups.sets[groupPath][groupId]) {
if(!choiceGroupMessagesAdded[requiredGroup]) {
choiceGroupMessagesAdded[requiredGroup] = true;
pushMessage(messages, 'requiredChoiceGroup', groupId, groupPath, [true], messageObject);
}
}
});
if(debug) {
console.log(messages);
}
if(callback) {
function generateMessageTree(messageObject) {
var messageTree = unflatten(messageObject);
return traverse(messageTree).forEach(function(node) {
if(typeof node !== 'object') {
return;
}
if(Array.isArray(node)) {
var children = node.length,
traversed = 0;
this.post(function() {
traversed++;
if(children === traversed) {
var newNode = node.filter(function(elemento) {
return elemento !== null && typeof elemento !== 'undefined'
});
if(newNode.length === 0) {
this.remove();
} else {
this.update(newNode);
}
}
});
}
var nodesToOutput = Object.keys(node).filter(function(key) {
return key.indexOf('__') === 0;
});
if(nodesToOutput.length > 0) {
var numberOfProperties = 0,
numberOfArrays = 0,
numberOfNonArrays = 0;
forEachOwnProperty(node, function(propertyName, object) {
numberOfProperties++;
if(Array.isArray(object) && typeof object[0] === 'object') {
numberOfArrays++;
} else {
numberOfNonArrays++;
}
});
if(numberOfProperties - numberOfArrays === nodesToOutput.length) {
nodesToOutput.forEach(function(key) {
delete node[key];
});
if(this.notRoot && JSON.stringify(node) === '{}') {
if(this.parent && Array.isArray(this.parent.node)) {
this.update(null);
if(this.parent.node.length === 0) {
this.parent.remove();
}
} else {
this.remove();
}
}
}
}
});
}
async.parallel(asyncValidations, function(err, asyncMessages) {
if(err) {
return callback(err);
}
var messageTree = generateMessageTree(messageObject),
isValid = JSON.stringify(messageTree) === '{}';
async.parallel(asyncTransformations, function(err, newValues) {
if(err) {
return callback(err);
}
newValues.forEach(function(transform) {
deepSet(object.value, transform.path, transform.newValue);
});
callback(null, messageTree, isValid, messages);
});
});
}
return messages;
}
function find(array, term) {
var found = false;
array.forEach( function(element) {
if(term.indexOf(element) !== -1) {
found = true;
}
});
return found;
}