This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
validators.js
152 lines (138 loc) · 3.83 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* This is a supposed to be a list of common validators
* that can be reused instead of creating new ones.
**/
var validators = {};
/**
* Check if a number is between a minimum and
* a maximum number. One of this constraints
* can be omitted.
**/
validators.rangeNumber = function (min, max) {
return function (n, next) {
if (min === undefined && n <= max) return next();
if (max === undefined && n >= min) return next();
if (n >= min && n <= max) return next();
return next('out-of-range-number');
};
};
/**
* Check if a string length is between a minimum
* and a maximum number. One of this constraints
* can be omitted.
**/
validators.rangeLength = function (min, max) {
return function (v, next) {
if (v === undefined) return next('undefined');
if (min === undefined && v.length <= max) return next();
if (max === undefined && v.length >= min) return next();
if (v.length >= min && v.length <= max) return next();
return next('out-of-range-length');
};
};
/**
* Check if a value (number or string) is
* in a list of values.
**/
validators.insideList = function (list) {
return function (v, next) {
if (list.indexOf(v) >= 0) return next();
return next('outside-list');
};
};
/**
* Check if a value (number or string) is
* not in a list of values.
**/
validators.outsideList = function (list) {
return function (v, next) {
if (list.indexOf(v) == -1) return next();
return next('inside-list');
};
};
/**
* Check if a value is the same as a value
* of another property (useful for password
* checking).
**/
validators.equalToProperty = function (name) {
return function (v, next, data) {
// could also do: v == this[name]
if (v == data[name]) return next();
return next('not-equal-to-property');
};
};
/**
* Check if a string has zero length. Sometimes
* you might want to have a property on your
* model that is not required but on a specific
* form it can be.
**/
validators.notEmptyString = function () {
return validators.rangeLength(1);
};
/**
* Check if a property is unique in the collection.
* This can take a while because a query has to be
* made against the Model, but if you use this
* always you should not have not unique values
* on this property so this should not worry you.
**/
validators.unique = function () {
return function (v, next, data, Model, prop) {
var query = {};
query[prop] = v;
Model.find(query, function (records) {
if (!records || records.length === 0) {
return next();
}
if (records.length == 1 && records[0].id === data.id) {
return next();
}
return next("not-unique");
});
};
};
/**
* Pattern validators are usually based on regular
* expressions and solve more complicated validations
* you might need.
**/
validators.patterns = {};
/**
* Check if a value matches a given pattern.
* You can define a pattern string and regex
* modifiers or just send the RegExp object
* as 1st argument.
**/
validators.patterns.match = function (pattern, modifiers) {
return function (v, next) {
if (typeof pattern == "string") {
pattern = new RegExp(pattern, modifiers);
}
if (typeof v == "string" && v.match(pattern)) return next();
return next('no-pattern-match');
};
};
/**
* Check if a value is an hexadecimal string
* (letters from A to F and numbers).
**/
validators.patterns.hexString = function () {
return validators.patterns.match("^[a-f0-9]+$", "i");
};
/**
* Check if a value is an e-mail address
* (simple checking, works 99%).
**/
validators.patterns.email = function () {
return validators.patterns.match("^[a-z0-9\\._%\\+\\-]+@[a-z0-9\\.\\-]+\\.[a-z]{2,6}$", "i");
};
/**
* Check if it's a valid IPv4 address.
**/
validators.patterns.ipv4 = function () {
var part = "(1[0-9]{,2}|2[0-4][0-9]|25[0-4])";
return validators.patterns.match("^" + [ part, part, part, part ].join("\\.") + "$", "");
};
module.exports = validators;