forked from sailshq/anchor
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrules.js
138 lines (117 loc) · 4.4 KB
/
rules.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
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
/**
* Module dependencies
*/
var _ = require('lodash');
var validator = require('validator');
var geojsonhint = require('@mapbox/geojsonhint');
/**
* Type rules
*/
module.exports = {
'empty' : _.isEmpty,
'required' : function (x) { return (x !== void 0 && x !== '' && !(_.isArray(x) && x.length === 0) && !_.isNull(x)); },
'protected' : function () { return true; },
'notEmpty' : function (x) { return !_.isEmpty(x); },
'undefined' : _.isUndefined,
'object' : _.isObject,
'json' : function (x) {
if (_.isUndefined(x)) { return false; }
try { JSON.stringify(x); }
catch (err) { return false; }
return true;
},
'geojson' : function (value) {
var errors = geojsonhint.hint(value);
return errors.length === 0;
},
'geometry': function (value) {
return _.isEmpty(value) || (_.isObject(value) || _.isString(value));
},
'mediumtext' : _.isString,
'longtext' : _.isString,
'text' : _.isString,
'string' : _.isString,
'alpha' : validator.isAlpha,
'alphadashed': function (x) {return (/^[a-zA-Z-_]*$/).test(x); },
'numeric': function (x) { return !isNaN(x); },
'alphanumeric': validator.isAlphanumeric,
'alphanumericdashed': function (x) {return (/^[a-zA-Z0-9-_]*$/).test(x); },
'email' : validator.isEmail,
'url' : function(x, opt) { return validator.isURL(x, opt === true ? undefined : opt); },
'urlish' : function (x) {
return !!validator.matches(x, /^\s([^\/]+\.)+.+\s*$/g);
},
'ip' : validator.isIP,
'ipv4' : validator.isIPv4,
'ipv6' : validator.isIPv6,
'creditcard': validator.isCreditCard,
'uuid' : validator.isUUID,
'uuidv3' : function (x){ return validator.isUUID(x, 3);},
'uuidv4' : function (x){ return validator.isUUID(x, 4);},
'int' : _.isInteger,
'integer' : _.isInteger,
'number' : _.isNumber,
'finite' : _.isFinite,
'decimal' : function (x) { return !isNaN(x); },
'float': function (x) { return !isNaN(x); },
'falsey' : function (x) { return !x; },
'truthy' : function (x) { return !!x; },
'null' : _.isNull,
'notNull' : function (x) { return !_.isNull(x); },
'boolean' : _.isBoolean,
'array' : _.isArray,
'binary' : function (x) { return Buffer.isBuffer(x) || _.isString(x); },
'date' : _.isDate,
'datetime': _.isDate,
'time' : _.isDate,
'hexadecimal': validator.isHexadecimal,
'hexColor': validator.isHexColor,
'lowercase': validator.isLowercase,
'uppercase': validator.isUppercase,
// Miscellaneous rules
'after': function(after, than) {
after = _.isDate(after) ? after : validator.toDate(after);
than = _.isDate(than) ? than : validator.toDate(than);
return (after > than);
},
'before': function(before, than) {
before = _.isDate(before) ? before : validator.toDate(before);
than = _.isDate(than) ? than : validator.toDate(than);
return (before < than);
},
'equals' : validator.equals,
'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;
},
'greaterThan' : function (x, val) {
var number = parseFloat(x);
return isNaN(number) || number > val;
},
'lessThan' : function (x, val) {
var number = parseFloat(x);
return isNaN(number) || number < val;
},
'minLength' : function (x, min) { return validator.isLength(x, min); },
'maxLength' : function (x, max) { return validator.isLength(x, 0, max); },
'is' : function (x, regex) {
if (!_.isRegExp(regex)) { throw new Error('`regex` (aka `is`) validation rule must be provided a true regular expression (instead got `'+typeof regex+'`)'); }
return validator.matches(x, regex);
},
'regex' : function (x, regex) {
if (!_.isRegExp(regex)) { throw new Error('`regex` validation rule must be provided a true regular expression (instead got `'+typeof regex+'`)'); }
return validator.matches(x, regex);
},
'notRegex' : function (x, regex) {
if (!_.isRegExp(regex)) { throw new Error('`notRegex` validation rule must be provided a true regular expression (instead got `'+typeof regex+'`)'); }
return !validator.matches(x, regex);
},
};