-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidators.js
81 lines (77 loc) · 2.44 KB
/
validators.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var _ = require('lodash'),
validator = require('validator'),
validators = {
//types
'string': _.isString,
'integer': validator.isInt,
'decimal': validator.isFloat,
'numeric': validator.isNumeric,
'boolean': _.isBoolean,
'email': validator.isEmail,
'url': validator.isURL,
'datetime': validator.isDate,
'array': _.isArray,
'object': _.isObject,
'json': function (x) {
//json is not object. it is serialized object.
if (_.isUndefined(x)) return false;
try {
JSON.parse(x);
}
catch (err) {
return false;
}
return true;
},
//validators
'minLength': function (x, min) {
return validator.isLength(x, min);
},
'maxLength': function (x, max) {
return validator.isLength(x, 0, max);
},
'after': validator.isAfter,
'before': validator.isBefore,
'contains': validator.contains,
'notContains': function (x, str) {
return !validator.contains(x, str);
},
'in': validator.isIn,
'notIn': function (x, arrayOrString) {
return !validator.isIn(x, arrayOrString);
},
'max': function (x, val) {
var number = parseFloat(x);
return isNaN(number) || number <= val;
},
'min': function (x, val) {
var number = parseFloat(x);
return isNaN(number) || number >= val;
},
'regex': function (x, regex) {
return validator.matches(x, regex);
},
'notRegex': function (x, regex) {
return !validator.matches(x, regex);
},
'notNull': function (x) {
return !validator.isNull(x);
},
//operators
'required': function (x) {
if (!x && x !== 0) x = '';
else if (typeof x.toString !== 'undefined') x = x.toString();
else x = '' + x;
return !validator.isNull(x);
},
'reject': _.isUndefined,
'default': function (x, defaults) {
if (_.isUndefined(x)) x = _.isFunction(defaults) ? defaults() : _.clone(defaults);
return x;
},
'transform': function (x, func) {
if (!_.isUndefined(x)) x = func(x);
return x;
}
};
module.exports = validators;