-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
109 lines (89 loc) · 3.08 KB
/
Copy pathindex.js
File metadata and controls
109 lines (89 loc) · 3.08 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
var validatorjs = require('validator');
var validate = (function() {
return function validate(validator, value) {
if(!validator) throw new Error("Must pass validator parameter");
if(!validator.validate) throw new Error("Validator must have validate attribute");
if(!Array.isArray(validator.validate)) throw new Error("Validator validate attribute must be an Array");
var valid = true;
var validate = validator.validate;
var result = {
valid: true,
messages: []
};
// if not required and no value passed, don't try to validate it
if(!validator.isRequired && !value) return result;
// transform numbers to string. Validatorjs only supports string validation
const _value = isNaN(value) ? value : value.toString();
for (var i = 0; i < validate.length; i++) {
if (validate[i].validator === 'undefined') { continue; }
var args = validate[i].arguments;
args = !Array.isArray(args) ? [args] : args;
var clonedArgs = args.slice(0);
clonedArgs.unshift(value || '');
var message = validate[i].message || '';
var title = validator.title;
if (title) {
message = message.replace('{TITLE}', title);
}
message = message.replace(/{ARGS\[(\d+)\]}/g, function (replace, argIndex) {
var val = args[argIndex];
return val !== undefined ? val : '';
});
if (typeof validate[i].validator === 'function') {
valid = validate[i].validator.apply(null, clonedArgs);
if(!valid) {
result.valid = false
result.messages.push(message)
}
} else {
if (typeof validatorjs[validate[i].validator] === 'undefined') {
console.warn('validate-model error: validator is not correct for: ' + validator);
continue;
}
if (validate[i].validator === 'isLength') {
if (typeof clonedArgs[0] === 'string') {
clonedArgs[0] = clonedArgs[0].trim();
}
}
valid = validatorjs[validate[i].validator].apply(null, clonedArgs);
if(!valid) {
result.valid = false
result.messages.push(message)
}
}
}
return result;
}
})();
var validateAll = (function() {
return function validateAll(validators, model) {
var result = {
valid: true,
messages: {}
};
for(var attribute in validators) {
var validation = validate(validators[attribute], model[attribute])
if (!validation.valid) {
result.valid = false;
result.messages[attribute] = validation.messages;
}
}
return result;
}
})();
var validateFunction = (function() {
return function validateFunction(validators) {
return function validate(values) {
var validation = validateAll(validators, values);
if (!validation.valid) return validation.messages;
return {};
}
}
})();
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
validate: validate,
validateAll: validateAll,
validateFunction: validateFunction
};
}