-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ValidationUtils.ts
473 lines (408 loc) · 14.4 KB
/
ValidationUtils.ts
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import {addYears, endOfMonth, format, isAfter, isBefore, isSameDay, isValid, isWithinInterval, parse, parseISO, startOfDay, subYears} from 'date-fns';
import {URL_REGEX_WITH_REQUIRED_PROTOCOL} from 'expensify-common/lib/Url';
import isDate from 'lodash/isDate';
import isEmpty from 'lodash/isEmpty';
import isObject from 'lodash/isObject';
import CONST from '@src/CONST';
import type {Report} from '@src/types/onyx';
import type * as OnyxCommon from '@src/types/onyx/OnyxCommon';
import * as CardUtils from './CardUtils';
import DateUtils from './DateUtils';
import * as LoginUtils from './LoginUtils';
import {parsePhoneNumber} from './PhoneNumber';
import StringUtils from './StringUtils';
/**
* Implements the Luhn Algorithm, a checksum formula used to validate credit card
* numbers.
*/
function validateCardNumber(value: string): boolean {
let sum = 0;
for (let i = 0; i < value.length; i++) {
let intVal = parseInt(value.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)
*/
function isValidAddress(value: string): boolean {
if (!CONST.REGEX.ANY_VALUE.test(value) || value.match(CONST.REGEX.EMOJIS)) {
return false;
}
return !CONST.REGEX.PO_BOX.test(value);
}
/**
* Validate date fields
*/
function isValidDate(date: string | Date): boolean {
if (!date) {
return false;
}
const pastDate = subYears(new Date(), 1000);
const futureDate = addYears(new Date(), 1000);
const testDate = new Date(date);
return isValid(testDate) && isAfter(testDate, pastDate) && isBefore(testDate, futureDate);
}
/**
* Validate that date entered isn't a future date.
*/
function isValidPastDate(date: string | Date): boolean {
if (!date) {
return false;
}
const pastDate = subYears(new Date(), 1000);
const currentDate = new Date();
const testDate = startOfDay(new Date(date));
return isValid(testDate) && isAfter(testDate, pastDate) && isBefore(testDate, currentDate);
}
/**
* Used to validate a value that is "required".
*/
function isRequiredFulfilled(value: string | Date | unknown[] | Record<string, unknown>): boolean {
if (typeof value === 'string') {
return !StringUtils.isEmptyString(value);
}
if (isDate(value)) {
return isValidDate(value);
}
if (Array.isArray(value) || isObject(value)) {
return !isEmpty(value);
}
return Boolean(value);
}
/**
* Used to add requiredField error to the fields passed.
*/
function getFieldRequiredErrors(values: OnyxCommon.Errors, requiredFields: string[]) {
const errors: OnyxCommon.Errors = {};
requiredFields.forEach((fieldKey) => {
if (isRequiredFulfilled(values[fieldKey])) {
return;
}
errors[fieldKey] = 'common.error.fieldRequired';
});
return errors;
}
/**
* Validates that this is a valid expiration date. Supports the following formats:
* 1. MM/YY
* 2. MM/YYYY
* 3. MMYY
* 4. MMYYYY
*/
function isValidExpirationDate(string: string): boolean {
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 isAfter(new Date(expirationDate), endOfMonth(new Date()));
}
/**
* Validates that this is a valid security code
* in the XXX or XXXX format.
*/
function isValidSecurityCode(string: string): boolean {
return CONST.REGEX.CARD_SECURITY_CODE.test(string);
}
/**
* Validates a debit card number (15 or 16 digits).
*/
function isValidDebitCard(string: string): boolean {
if (!CONST.REGEX.CARD_NUMBER.test(string)) {
return false;
}
return validateCardNumber(string);
}
function isValidIndustryCode(code: string): boolean {
return CONST.REGEX.INDUSTRY_CODE.test(code);
}
function isValidZipCode(zipCode: string): boolean {
return CONST.REGEX.ZIP_CODE.test(zipCode);
}
function isValidSSNLastFour(ssnLast4: string): boolean {
return CONST.REGEX.SSN_LAST_FOUR.test(ssnLast4);
}
function isValidSSNFullNine(ssnFull9: string): boolean {
return CONST.REGEX.SSN_FULL_NINE.test(ssnFull9);
}
/**
* Validate that a date meets the minimum age requirement.
*/
function meetsMinimumAgeRequirement(date: string): boolean {
const testDate = new Date(date);
const minDate = subYears(new Date(), CONST.DATE_BIRTH.MIN_AGE_FOR_PAYMENT);
return isValid(testDate) && (isSameDay(testDate, minDate) || isBefore(testDate, minDate));
}
/**
* Validate that a date meets the maximum age requirement.
*/
function meetsMaximumAgeRequirement(date: string): boolean {
const testDate = new Date(date);
const maxDate = subYears(new Date(), CONST.DATE_BIRTH.MAX_AGE);
return isValid(testDate) && (isSameDay(testDate, maxDate) || isAfter(testDate, maxDate));
}
/**
* Validate that given date is in a specified range of years before now.
*/
function getAgeRequirementError(date: string, minimumAge: number, maximumAge: number): string | Array<string | Record<string, string>> {
const currentDate = startOfDay(new Date());
const testDate = parse(date, CONST.DATE.FNS_FORMAT_STRING, currentDate);
if (!isValid(testDate)) {
return 'common.error.dateInvalid';
}
const maximalDate = subYears(currentDate, minimumAge);
const minimalDate = subYears(currentDate, maximumAge);
if (isWithinInterval(testDate, {start: minimalDate, end: maximalDate})) {
return '';
}
if (isSameDay(testDate, maximalDate) || isAfter(testDate, maximalDate)) {
return ['privatePersonalDetails.error.dateShouldBeBefore', {dateString: format(maximalDate, CONST.DATE.FNS_FORMAT_STRING)}];
}
return ['privatePersonalDetails.error.dateShouldBeAfter', {dateString: format(minimalDate, CONST.DATE.FNS_FORMAT_STRING)}];
}
/**
* Validate that given date is not in the past.
*/
function getDatePassedError(inputDate: string): string {
const currentDate = new Date();
const parsedDate = new Date(`${inputDate}T00:00:00`); // set time to 00:00:00 for accurate comparison
// If input date is not valid, return an error
if (!isValid(parsedDate)) {
return 'common.error.dateInvalid';
}
// Clear time for currentDate so comparison is based solely on the date
currentDate.setHours(0, 0, 0, 0);
if (parsedDate < currentDate) {
return 'common.error.dateInvalid';
}
return '';
}
/**
* Similar to backend, checks whether a website has a valid URL or not.
* http/https/ftp URL scheme required.
*/
function isValidWebsite(url: string): boolean {
const isLowerCase = url === url.toLowerCase();
return new RegExp(`^${URL_REGEX_WITH_REQUIRED_PROTOCOL}$`, 'i').test(url) && isLowerCase;
}
function validateIdentity(identity: Record<string, string>): Record<string, boolean> {
const requiredFields = ['firstName', 'lastName', 'street', 'city', 'zipCode', 'state', 'ssnLast4', 'dob'];
const errors: Record<string, boolean> = {};
// Check that all required fields are filled
requiredFields.forEach((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) || !meetsMaximumAgeRequirement(identity.dob)) {
errors.dob = true;
} else if (!meetsMinimumAgeRequirement(identity.dob)) {
errors.dobAge = true;
}
if (!isValidSSNLastFour(identity.ssnLast4)) {
errors.ssnLast4 = true;
}
return errors;
}
function isValidUSPhone(phoneNumber = '', isCountryCodeOptional?: boolean): boolean {
const phone = phoneNumber || '';
const regionCode = isCountryCodeOptional ? CONST.COUNTRY.US : undefined;
const parsedPhoneNumber = parsePhoneNumber(phone, {regionCode});
return parsedPhoneNumber.possible && parsedPhoneNumber.regionCode === CONST.COUNTRY.US;
}
function isValidValidateCode(validateCode: string): boolean {
return Boolean(validateCode.match(CONST.VALIDATE_CODE_REGEX_STRING));
}
function isValidRecoveryCode(recoveryCode: string): boolean {
return Boolean(recoveryCode.match(CONST.RECOVERY_CODE_REGEX_STRING));
}
function isValidTwoFactorCode(code: string): boolean {
return Boolean(code.match(CONST.REGEX.CODE_2FA));
}
/**
* Checks whether a value is a numeric string including `(`, `)`, `-` and optional leading `+`
*/
function isNumericWithSpecialChars(input: string): boolean {
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/
*/
function isValidRoutingNumber(routingNumber: string): boolean {
let n = 0;
for (let i = 0; i < routingNumber.length; i += 3) {
n += parseInt(routingNumber.charAt(i), 10) * 3 + parseInt(routingNumber.charAt(i + 1), 10) * 7 + parseInt(routingNumber.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 that the provided name doesn't contain any emojis
*/
function isValidCompanyName(name: string) {
return !name.match(CONST.REGEX.EMOJIS);
}
/**
* Checks that the provided name doesn't contain any commas or semicolons
*/
function isValidDisplayName(name: string): boolean {
return !name.includes(',') && !name.includes(';');
}
/**
* Checks that the provided legal name doesn't contain special characters
*/
function isValidLegalName(name: string): boolean {
const hasAccentedChars = Boolean(name.match(CONST.REGEX.ACCENT_LATIN_CHARS));
return CONST.REGEX.ALPHABETIC_AND_LATIN_CHARS.test(name) && !hasAccentedChars;
}
/**
* Checks that the provided name doesn't contain special characters or numbers
*/
function isValidPersonName(value: string) {
return /^[^\d^!#$%*=<>;{}"]+$/.test(value);
}
/**
* Checks if the provided string includes any of the provided reserved words
*/
function doesContainReservedWord(value: string, reservedWords: string[]): boolean {
const valueToCheck = value.trim().toLowerCase();
return reservedWords.some((reservedWord) => valueToCheck.includes(reservedWord.toLowerCase()));
}
/**
* Checks if is one of the certain names which are reserved for default rooms
* and should not be used for policy rooms.
*/
function isReservedRoomName(roomName: string): boolean {
return (CONST.REPORT.RESERVED_ROOM_NAMES as readonly string[]).includes(roomName);
}
/**
* Checks if the room name already exists.
*/
function isExistingRoomName(roomName: string, reports: Record<string, Report>, policyID: string): boolean {
return Object.values(reports).some((report) => report && report.policyID === policyID && report.reportName === roomName);
}
/**
* Checks if a room name is valid by checking that:
* - It starts with a hash '#'
* - After the first character, it contains only lowercase letters, numbers, and dashes
* - It's between 1 and MAX_ROOM_NAME_LENGTH characters long
*/
function isValidRoomName(roomName: string): boolean {
return CONST.REGEX.ROOM_NAME.test(roomName);
}
/**
* Checks if tax ID consists of 9 digits
*/
function isValidTaxID(taxID: string): boolean {
return CONST.REGEX.TAX_ID.test(taxID);
}
/**
* Checks if a string value is a number.
*/
function isNumeric(value: string): boolean {
if (typeof value !== 'string') {
return false;
}
return /^\d*$/.test(value);
}
/**
* Checks that the provided accountID is a number and bigger than 0.
*/
function isValidAccountRoute(accountID: number): boolean {
return CONST.REGEX.NUMBER.test(String(accountID)) && accountID > 0;
}
/**
* Validates that the date and time are at least one minute in the future.
* data - A date and time string in 'YYYY-MM-DD HH:mm:ss.sssZ' format
* returns an object containing the error messages for the date and time
*/
const validateDateTimeIsAtLeastOneMinuteInFuture = (data: string): {dateValidationErrorKey: string; timeValidationErrorKey: string} => {
if (!data) {
return {
dateValidationErrorKey: '',
timeValidationErrorKey: '',
};
}
const parsedInputData = parseISO(data);
const dateValidationErrorKey = DateUtils.getDayValidationErrorKey(parsedInputData);
const timeValidationErrorKey = DateUtils.getTimeValidationErrorKey(parsedInputData);
return {
dateValidationErrorKey,
timeValidationErrorKey,
};
};
type ValuesType = Record<string, unknown>;
/**
* This function is used to remove invisible characters from strings before validation and submission.
*/
function prepareValues(values: ValuesType): ValuesType {
const trimmedStringValues: ValuesType = {};
for (const [inputID, inputValue] of Object.entries(values)) {
if (typeof inputValue === 'string') {
trimmedStringValues[inputID] = StringUtils.removeInvisibleCharacters(inputValue);
} else {
trimmedStringValues[inputID] = inputValue;
}
}
return trimmedStringValues;
}
export {
meetsMinimumAgeRequirement,
meetsMaximumAgeRequirement,
getAgeRequirementError,
isValidAddress,
isValidDate,
isValidPastDate,
isValidSecurityCode,
isValidExpirationDate,
isValidDebitCard,
isValidIndustryCode,
isValidZipCode,
isRequiredFulfilled,
getFieldRequiredErrors,
isValidUSPhone,
isValidWebsite,
validateIdentity,
isValidTwoFactorCode,
isNumericWithSpecialChars,
isValidRoutingNumber,
isValidSSNLastFour,
isValidSSNFullNine,
isReservedRoomName,
isExistingRoomName,
isValidRoomName,
isValidTaxID,
isValidValidateCode,
isValidCompanyName,
isValidDisplayName,
isValidLegalName,
doesContainReservedWord,
isNumeric,
isValidAccountRoute,
getDatePassedError,
isValidRecoveryCode,
validateDateTimeIsAtLeastOneMinuteInFuture,
prepareValues,
isValidPersonName,
};