-
Notifications
You must be signed in to change notification settings - Fork 533
/
Copy pathvalidators.js
230 lines (177 loc) · 6.07 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { isNil, isNumber, isString, isArray } from "lodash";
import moment from "moment/min/moment.min";
function checkEmpty(value, required) {
if (isNil(value) || value === "") {
if (required)
return [msg(resources.fieldIsRequired)];
else
return [];
}
return null;
}
function msg(text) {
if (text != null && arguments.length > 1)
for (let i = 1; i < arguments.length; i++)
text = text.replace(/\{\d+?\}/, arguments[i]);
return text;
}
let resources = {
fieldIsRequired: "This field is required!",
invalidFormat: "Invalid format!",
numberTooSmall: "The number is too small! Minimum: {0}",
numberTooBig: "The number is too big! Maximum: {0}",
invalidNumber: "Invalid number",
textTooSmall: "The length of text is too small! Current: {0}, Minimum: {1}",
textTooBig: "The length of text is too big! Current: {0}, Maximum: {1}",
thisNotText: "This is not a text!",
thisNotArray: "This is not an array!",
selectMinItems: "Select minimum {0} items!",
selectMaxItems: "Select maximum {0} items!",
invalidDate: "Invalid date!",
dateIsEarly: "The date is too early! Current: {0}, Minimum: {1}",
dateIsLate: "The date is too late! Current: {0}, Maximum: {1}",
invalidEmail: "Invalid e-mail address!",
invalidURL: "Invalid URL!",
invalidCard: "Invalid card format!",
invalidCardNumber: "Invalid card number!",
invalidTextContainNumber: "Invalid text! Cannot contains numbers or special characters",
invalidTextContainSpec: "Invalid text! Cannot contains special characters"
};
module.exports = {
resources,
required(value, field) {
return checkEmpty(value, field.required);
},
number(value, field) {
let res = checkEmpty(value, field.required); if (res != null) return res;
let err = [];
if (isNumber(value)) {
if (!isNil(field.min) && value < field.min)
err.push(msg(resources.numberTooSmall, field.min));
if (!isNil(field.max) && value > field.max)
err.push(msg(resources.numberTooBig, field.max));
} else
err.push(msg(resources.invalidNumber));
return err;
},
integer(value, field) {
let res = checkEmpty(value, field.required); if (res != null) return res;
if (!(Number(value) === value && value % 1 === 0))
return [msg(resources.invalidNumber)];
},
double(value, field) {
let res = checkEmpty(value, field.required); if (res != null) return res;
if (!(Number(value) === value && value % 1 !== 0))
return [msg(resources.invalidNumber)];
},
string(value, field) {
let res = checkEmpty(value, field.required); if (res != null) return res;
let err = [];
if (isString(value)) {
if (!isNil(field.min) && value.length < field.min)
err.push(msg(resources.textTooSmall, value.length, field.min));
if (!isNil(field.max) && value.length > field.max)
err.push(msg(resources.textTooBig, value.length, field.max));
} else
err.push(msg(resources.thisNotText));
return err;
},
array(value, field) {
if (field.required) {
if (!isArray(value))
return [msg(resources.thisNotArray)];
if (value.length == 0)
return [msg(resources.fieldIsRequired)];
}
if (!isNil(value)) {
if (!isNil(field.min))
if (value.length < field.min)
return [msg(resources.selectMinItems, field.min)];
if (!isNil(field.max))
if (value.length > field.max)
return [msg(resources.selectMaxItems, field.max)];
}
},
date(value, field) {
let res = checkEmpty(value, field.required); if (res != null) return res;
let m = moment(value);
if (!m.isValid())
return [msg(resources.invalidDate)];
let err = [];
if (!isNil(field.min)) {
let min = moment(field.min);
if (m.isBefore(min))
err.push(msg(resources.dateIsEarly, m.format("L"), min.format("L")));
}
if (!isNil(field.max)) {
let max = moment(field.max);
if (m.isAfter(max))
err.push(msg(resources.dateIsLate, m.format("L"), max.format("L")));
}
return err;
},
regexp(value, field) {
let res = checkEmpty(value, field.required); if (res != null) return res;
if (!isNil(field.pattern)) {
let re = new RegExp(field.pattern);
if (!re.test(value))
return [msg(resources.invalidFormat)];
}
},
email(value, field) {
let res = checkEmpty(value, field.required); if (res != null) return res;
let re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (!re.test(value))
return [msg(resources.invalidEmail)];
},
url(value, field) {
let res = checkEmpty(value, field.required); if (res != null) return res;
let re = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g;
if (!re.test(value))
return [msg(resources.invalidURL)];
},
creditCard(value, field) {
let res = checkEmpty(value, field.required); if (res != null) return res;
/* From validator.js code
https://github.com/chriso/validator.js/blob/master/src/lib/isCreditCard.js
*/
const creditCard = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/;
const sanitized = value.replace(/[^0-9]+/g, "");
if (!creditCard.test(sanitized)) {
return [msg(resources.invalidCard)];
}
let sum = 0;
let digit;
let tmpNum;
let shouldDouble;
for (let i = sanitized.length - 1; i >= 0; i--) {
digit = sanitized.substring(i, (i + 1));
tmpNum = parseInt(digit, 10);
if (shouldDouble) {
tmpNum *= 2;
if (tmpNum >= 10) {
sum += ((tmpNum % 10) + 1);
} else {
sum += tmpNum;
}
} else {
sum += tmpNum;
}
shouldDouble = !shouldDouble;
}
if (!((sum % 10) === 0 ? sanitized : false))
return [msg(resources.invalidCardNumber)];
},
alpha(value, field) {
let res = checkEmpty(value, field.required); if (res != null) return res;
let re = /^[a-zA-Z]*$/;
if (!re.test(value))
return [msg(resources.invalidTextContainNumber)];
},
alphaNumeric(value, field) {
let res = checkEmpty(value, field.required); if (res != null) return res;
let re = /^[a-zA-Z0-9]*$/;
if (!re.test(value))
return [msg(resources.invalidTextContainSpec)];
}
};