-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
46 lines (42 loc) · 1.07 KB
/
validate.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
const validators = {
isDate: function(date, errMsg) {
if (typeof date === "undefined" || new Date(date) == "Invalid Date") {
return errMsg;
}
},
isEmptyString: function(data, errMsg) {
if(typeof data === "undefined" || data.trim().length === 0) {
return errMsg
}
},
isArray: function(arr, errMsg) {
if(Object.prototype.toString.call(arr) !== "[object array]") {
return errMsg;
}
},
isObject: function(obj, errMsg) {
if(Object.prototype.toString.call(obj) !== "[object object]" || JSON.stringify(obj) !== '{}') {
return errMsg
}
}
};
function Validate() {
this.cache = [];
}
Validate.prototype.add = function(rules, value, errMsg) {
let ary = rules.split(":");
this.cache.push(function() {
var strategory = ary.shift();
ary.unshift(value);
ary.push(errMsg);
return validators[strategory].apply(null, ary);
})
}
Validate.prototype.start = function() {
const { cache } = this;
for(var i=0, fn; fn=cache[i++]; ) {
var errMsg = fn();
if(errMsg) return errMsg;
}
}
export default Validate;