-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathValidationUtils.js
384 lines (344 loc) · 9.14 KB
/
ValidationUtils.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import moment from 'moment';
import _ from 'underscore';
import CONST from '../CONST';
import * as CardUtils from './CardUtils';
import * as LoginUtils from './LoginUtils';
/**
* Implements the Luhn Algorithm, a checksum formula used to validate credit card
* numbers.
*
* @param {String} val
* @returns {Boolean}
*/
function validateCardNumber(val) {
let sum = 0;
for (let i = 0; i < val.length; i++) {
let intVal = parseInt(val.substr(i, 1), 10);
if (i % 2 === 0) {
intVal *= 2;
if (intVal > 9) {
intVal = 1 + (intVal % 10);
}
}
sum += intVal;
}
return (sum % 10) === 0;
}
/**
* Validating that this is a valid address (PO boxes are not allowed)
*
* @param {String} value
* @returns {Boolean}
*/
function isValidAddress(value) {
if (!CONST.REGEX.ANY_VALUE.test(value)) {
return false;
}
return !CONST.REGEX.PO_BOX.test(value);
}
/**
* Validate date fields
*
* @param {String|Date} date
* @returns {Boolean} true if valid
*/
function isValidDate(date) {
if (!date) {
return false;
}
const pastDate = moment().subtract(1000, 'years');
const futureDate = moment().add(1000, 'years');
const testDate = moment(date);
return testDate.isValid() && testDate.isBetween(pastDate, futureDate);
}
/**
* Validate that date entered isn't a future date.
*
* @param {String|Date} date
* @returns {Boolean} true if valid
*/
function isValidPastDate(date) {
if (!date) {
return false;
}
const pastDate = moment().subtract(1000, 'years');
const currentDate = moment();
const testDate = moment(date).startOf('day');
return testDate.isValid() && testDate.isBetween(pastDate, currentDate);
}
/**
* Used to validate a value that is "required".
*
* @param {*} value
* @returns {Boolean}
*/
function isRequiredFulfilled(value) {
if (_.isString(value)) {
return !_.isEmpty(value.trim());
}
if (_.isDate(value)) {
return isValidDate(value);
}
if (_.isArray(value) || _.isObject(value)) {
return !_.isEmpty(value);
}
return Boolean(value);
}
/**
* Validates that this is a valid expiration date. Supports the following formats:
* 1. MM/YY
* 2. MM/YYYY
* 3. MMYY
* 4. MMYYYY
*
* @param {String} string
* @returns {Boolean}
*/
function isValidExpirationDate(string) {
if (!CONST.REGEX.CARD_EXPIRATION_DATE.test(string)) {
return false;
}
// Use the last of the month to check if the expiration date is in the future or not
const expirationDate = `${CardUtils.getYearFromExpirationDateString(string)}-${CardUtils.getMonthFromExpirationDateString(string)}-01`;
return moment(expirationDate).endOf('month').isAfter(moment());
}
/**
* Validates that this is a valid security code
* in the XXX or XXXX format.
*
* @param {String} string
* @returns {Boolean}
*/
function isValidSecurityCode(string) {
return CONST.REGEX.CARD_SECURITY_CODE.test(string);
}
/**
* Validates a debit card number (15 or 16 digits).
*
* @param {String} string
* @returns {Boolean}
*/
function isValidDebitCard(string) {
if (!CONST.REGEX.CARD_NUMBER.test(string)) {
return false;
}
return validateCardNumber(string);
}
/**
*
* @param {String} nameOnCard
* @returns {Boolean}
*/
function isValidCardName(nameOnCard) {
if (!CONST.REGEX.ALPHABETIC_CHARS.test(nameOnCard)) {
return false;
}
return !_.isEmpty(nameOnCard.trim());
}
/**
* @param {String} code
* @returns {Boolean}
*/
function isValidIndustryCode(code) {
return CONST.REGEX.INDUSTRY_CODE.test(code);
}
/**
* @param {String} zipCode
* @returns {Boolean}
*/
function isValidZipCode(zipCode) {
return CONST.REGEX.ZIP_CODE.test(zipCode);
}
/**
* @param {String} ssnLast4
* @returns {Boolean}
*/
function isValidSSNLastFour(ssnLast4) {
return CONST.REGEX.SSN_LAST_FOUR.test(ssnLast4);
}
/**
* @param {String} ssnFull9
* @returns {Boolean}
*/
function isValidSSNFullNine(ssnFull9) {
return CONST.REGEX.SSN_FULL_NINE.test(ssnFull9);
}
/**
*
* @param {String} paypalUsername
* @returns {Boolean}
*/
function isValidPaypalUsername(paypalUsername) {
return Boolean(paypalUsername) && CONST.REGEX.PAYPAL_ME_USERNAME.test(paypalUsername);
}
/**
* Validate that "date" is between 18 and 150 years in the past
*
* @param {String} date
* @returns {Boolean}
*/
function meetsAgeRequirements(date) {
const eighteenYearsAgo = moment().subtract(18, 'years');
const oneHundredFiftyYearsAgo = moment().subtract(150, 'years');
const testDate = moment(date);
return testDate.isValid() && testDate.isBetween(oneHundredFiftyYearsAgo, eighteenYearsAgo);
}
/**
*
* @param {String} phoneNumber
* @returns {Boolean}
*/
function isValidPhoneWithSpecialChars(phoneNumber) {
return CONST.REGEX.PHONE_WITH_SPECIAL_CHARS.test(phoneNumber) && phoneNumber.length <= CONST.PHONE_MAX_LENGTH && phoneNumber.length >= CONST.PHONE_MIN_LENGTH;
}
/**
* @param {String} url
* @returns {Boolean}
*/
function isValidURL(url) {
return CONST.REGEX.HYPERLINK.test(url);
}
/**
* @param {Object} identity
* @returns {Object}
*/
function validateIdentity(identity) {
const requiredFields = ['firstName', 'lastName', 'street', 'city', 'zipCode', 'state', 'ssnLast4', 'dob'];
const errors = {};
// Check that all required fields are filled
_.each(requiredFields, (fieldName) => {
if (isRequiredFulfilled(identity[fieldName])) {
return;
}
errors[fieldName] = true;
});
if (!isValidAddress(identity.street)) {
errors.street = true;
}
if (!isValidZipCode(identity.zipCode)) {
errors.zipCode = true;
}
// dob field has multiple validations/errors, we are handling it temporarily like this.
if (!isValidDate(identity.dob)) {
errors.dob = true;
} else if (!meetsAgeRequirements(identity.dob)) {
errors.dobAge = true;
}
if (!isValidSSNLastFour(identity.ssnLast4)) {
errors.ssnLast4 = true;
}
return errors;
}
/**
* @param {String} phoneNumber
* @returns {Boolean}
*/
function isValidUSPhone(phoneNumber) {
// Remove alphanumeric characters and validate that this is in fact a phone number
return CONST.REGEX.PHONE_E164_PLUS.test(phoneNumber.replace(CONST.REGEX.NON_ALPHA_NUMERIC, '')) && CONST.REGEX.US_PHONE.test(phoneNumber);
}
/**
* @param {String} password
* @returns {Boolean}
*/
function isValidPassword(password) {
return password.match(CONST.PASSWORD_COMPLEXITY_REGEX_STRING);
}
/**
* @param {String} input
* @returns {Boolean}
*/
function isPositiveInteger(input) {
return CONST.REGEX.POSITIVE_INTEGER.test(input);
}
/**
* Checks whether a value is a numeric string including `(`, `)`, `-` and optional leading `+`
* @param {String} input
* @returns {Boolean}
*/
function isNumericWithSpecialChars(input) {
return /^\+?\d*$/.test(LoginUtils.getPhoneNumberWithoutSpecialChars(input));
}
/**
* Checks the given number is a valid US Routing Number
* using ABA routingNumber checksum algorithm: http://www.brainjar.com/js/validation/
* @param {String} number
* @returns {Boolean}
*/
function isValidRoutingNumber(number) {
let n = 0;
for (let i = 0; i < number.length; i += 3) {
n += (parseInt(number.charAt(i), 10) * 3)
+ (parseInt(number.charAt(i + 1), 10) * 7)
+ parseInt(number.charAt(i + 2), 10);
}
// If the resulting sum is an even multiple of ten (but not zero),
// the ABA routing number is valid.
if (n !== 0 && n % 10 === 0) {
return true;
}
return false;
}
/**
* Checks if each string in array is of valid length and then returns true
* for each string which exceeds the limit.
*
* @param {Number} maxLength
* @param {String[]} valuesToBeValidated
* @returns {Boolean[]}
*/
function doesFailCharacterLimit(maxLength, valuesToBeValidated) {
return _.map(valuesToBeValidated, value => value.length > maxLength);
}
/**
* Checks if is one of the certain names which are reserved for default rooms
* and should not be used for policy rooms.
*
* @param {String} roomName
* @returns {Boolean}
*/
function isReservedRoomName(roomName) {
return _.contains(CONST.REPORT.RESERVED_ROOM_NAMES, roomName);
}
/**
* Checks if the room name already exists.
*
* @param {String} roomName
* @param {Object} reports
* @param {String} policyID
* @returns {Boolean}
*/
function isExistingRoomName(roomName, reports, policyID) {
return _.some(
reports,
report => report && report.policyID === policyID
&& report.reportName === roomName,
);
}
export {
meetsAgeRequirements,
isValidAddress,
isValidDate,
isValidCardName,
isValidPastDate,
isValidSecurityCode,
isValidExpirationDate,
isValidDebitCard,
isValidIndustryCode,
isValidZipCode,
isRequiredFulfilled,
isValidPhoneWithSpecialChars,
isValidUSPhone,
isValidURL,
validateIdentity,
isValidPassword,
isPositiveInteger,
isNumericWithSpecialChars,
isValidPaypalUsername,
isValidRoutingNumber,
isValidSSNLastFour,
isValidSSNFullNine,
doesFailCharacterLimit,
isReservedRoomName,
isExistingRoomName,
};