-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
57 lines (49 loc) · 1.63 KB
/
index.js
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
var validates = require("./rules/validates");
module.exports = function (values, rules, messages) {
var _rules = rules;
var _values = values;
var _messages = messages;
var _messagesDefault = {
required : "Required field",
maxlength : "Maximum character size of the field.",
minlength : "Minimum character size of the field.",
max : "The maximum value of the field exceeded.",
min : "The minimum field value exceeded.",
email : "Invalid email.",
url : "Invalid link.",
regexp : "Invalid field.",
equals : "This field must be identical to the corresponding field."
}
this.init = new Promise(function(resolve, reject) {
var splitRules = new Promise(function(resolve, reject) {
var rules = new Object();
Object.keys(_rules).map(function(key) {
rules[key] = _rules[key].split("|");
});
resolve(rules);
});
splitRules.then(function(rules) {
var isValid = true;
var errors = new Object();
Object.keys(rules).map(function(key) {
var msgs = new Array();
rules[key].map(function(rule) {
var name = rule;
var param = null;
if(rule.indexOf(":") != -1) {
name = rule.substring(0, rule.indexOf(":"));
param = rule.substring(rule.indexOf(":") + 1, rule.length);
}
if(!validates[name](_values, _values[key], param)) {
if(_messages[key+"."+name] != null) msgs.push(_messages[key+"."+name]);
else msgs.push(_messagesDefault[name]);
isValid = false;
}
});
errors[key] = msgs;
});
if(isValid) resolve({validated: true, values: _values});
else return resolve({validated: false, errors: errors, values: _values});
});
});
}