From eb9be0b5535b6919bca88d2b7d2a23ab20bf2260 Mon Sep 17 00:00:00 2001 From: tienifr Date: Tue, 19 Sep 2023 00:52:19 +0700 Subject: [PATCH 1/6] fix: 27390 --- src/libs/ValidationUtils.js | 5 +++++ src/pages/ReimbursementAccount/RequestorStep.js | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/src/libs/ValidationUtils.js b/src/libs/ValidationUtils.js index 81b91e2101be..157f66712334 100644 --- a/src/libs/ValidationUtils.js +++ b/src/libs/ValidationUtils.js @@ -28,6 +28,10 @@ function validateCardNumber(val) { return sum % 10 === 0; } +function isValidName(value) { + return /^[^\d\s!@#$%^&*(),.?":{}|<>]+$/.test(value); +} + /** * Validating that this is a valid address (PO boxes are not allowed) * @@ -489,4 +493,5 @@ export { doesContainReservedWord, isNumeric, isValidAccountRoute, + isValidName }; diff --git a/src/pages/ReimbursementAccount/RequestorStep.js b/src/pages/ReimbursementAccount/RequestorStep.js index 9831f4b8190c..8d322fcf82ef 100644 --- a/src/pages/ReimbursementAccount/RequestorStep.js +++ b/src/pages/ReimbursementAccount/RequestorStep.js @@ -41,6 +41,14 @@ class RequestorStep extends React.Component { const requiredFields = ['firstName', 'lastName', 'dob', 'ssnLast4', 'requestorAddressStreet', 'requestorAddressCity', 'requestorAddressState', 'requestorAddressZipCode']; const errors = ValidationUtils.getFieldRequiredErrors(values, requiredFields); + if (values.firstName && !ValidationUtils.isValidName(values.firstName)) { + errors.firstName = 'bankAccount.error.firstName'; + } + + if (values.lastName && !ValidationUtils.isValidName(values.lastName)) { + errors.lastName = 'bankAccount.error.lastName'; + } + if (values.dob) { if (!ValidationUtils.isValidPastDate(values.dob) || !ValidationUtils.meetsMaximumAgeRequirement(values.dob)) { errors.dob = 'bankAccount.error.dob'; From 233344ff51562704676d3de3ce43023de9c5ea87 Mon Sep 17 00:00:00 2001 From: tienifr Date: Wed, 1 Nov 2023 16:26:57 +0700 Subject: [PATCH 2/6] add type --- src/libs/ValidationUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/ValidationUtils.ts b/src/libs/ValidationUtils.ts index 47a50ccbf61d..6d9a2a26112a 100644 --- a/src/libs/ValidationUtils.ts +++ b/src/libs/ValidationUtils.ts @@ -29,7 +29,7 @@ function validateCardNumber(value: string): boolean { return sum % 10 === 0; } -function isValidName(value) { +function isValidName(value: string) { return /^[^\d\s!@#$%^&*(),.?":{}|<>]+$/.test(value); } From 7984e1f1f7502645c3354a014a914b94a5766a33 Mon Sep 17 00:00:00 2001 From: tienifr Date: Tue, 14 Nov 2023 20:49:47 +0700 Subject: [PATCH 3/6] fix merge errors --- src/libs/ValidationUtils.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/libs/ValidationUtils.ts b/src/libs/ValidationUtils.ts index 880be9067052..9aee8d2f0d48 100644 --- a/src/libs/ValidationUtils.ts +++ b/src/libs/ValidationUtils.ts @@ -78,7 +78,7 @@ function isValidPastDate(date: string | Date): boolean { */ function isRequiredFulfilled(value: string | Date | unknown[] | Record): boolean { if (typeof value === 'string') { - return value.trim().length > 0; + return !StringUtils.isEmptyString(value); } if (isDate(value)) { @@ -357,6 +357,25 @@ function isValidAccountRoute(accountID: number): boolean { return CONST.REGEX.NUMBER.test(String(accountID)) && accountID > 0; } +type ValuesType = Record; + +/** + * 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, From 03951e6c43f67d5dfbf58be7bbc60d8360e5421b Mon Sep 17 00:00:00 2001 From: tienifr Date: Mon, 20 Nov 2023 13:34:47 +0700 Subject: [PATCH 4/6] add tests --- src/libs/ValidationUtils.ts | 13 ++++++++----- tests/unit/ValidationUtilsTest.js | 13 +++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/libs/ValidationUtils.ts b/src/libs/ValidationUtils.ts index 9aee8d2f0d48..55043e8a0120 100644 --- a/src/libs/ValidationUtils.ts +++ b/src/libs/ValidationUtils.ts @@ -30,10 +30,6 @@ function validateCardNumber(value: string): boolean { return sum % 10 === 0; } -function isValidName(value: string) { - return /^[^\d\s!@#$%^&*(),.?":{}|<>]+$/.test(value); -} - /** * Validating that this is a valid address (PO boxes are not allowed) */ @@ -300,6 +296,13 @@ function isValidLegalName(name: string): boolean { return CONST.REGEX.ALPHABETIC_AND_LATIN_CHARS.test(name); } +/** + * 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 */ @@ -410,5 +413,5 @@ export { isValidAccountRoute, isValidRecoveryCode, prepareValues, - isValidName, + isValidPersonName, }; diff --git a/tests/unit/ValidationUtilsTest.js b/tests/unit/ValidationUtilsTest.js index bdc1f11948ae..57a67a2a740e 100644 --- a/tests/unit/ValidationUtilsTest.js +++ b/tests/unit/ValidationUtilsTest.js @@ -313,4 +313,17 @@ describe('ValidationUtils', () => { expect(ValidationUtils.isValidAccountRoute('123aaa')).toBe(false); }); }); + + describe('ValidatePersonName', () => { + test('Valid person name', () => { + expect(ValidationUtils.isValidPersonName('test name')).toBe(true); + expect(ValidationUtils.isValidPersonName(`X Æ A test`)).toBe(true); + }); + + test('Invalid person name', () => { + expect(ValidationUtils.isValidPersonName('123 test')).toBe(false); + expect(ValidationUtils.isValidPersonName('test #$')).toBe(false); + expect(ValidationUtils.isValidPersonName('test123$')).toBe(false); + }); + }); }); From 0d0fd0d573617c6340a10e45398fc3a66744a660 Mon Sep 17 00:00:00 2001 From: tienifr Date: Mon, 20 Nov 2023 13:35:16 +0700 Subject: [PATCH 5/6] change name --- src/pages/ReimbursementAccount/RequestorStep.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pages/ReimbursementAccount/RequestorStep.js b/src/pages/ReimbursementAccount/RequestorStep.js index 86bc56bc91fb..d0b8789658bf 100644 --- a/src/pages/ReimbursementAccount/RequestorStep.js +++ b/src/pages/ReimbursementAccount/RequestorStep.js @@ -52,11 +52,11 @@ const validate = (values) => { } } - if (values.firstName && !ValidationUtils.isValidName(values.firstName)) { + if (values.firstName && !ValidationUtils.isValidPersonName(values.firstName)) { errors.firstName = 'bankAccount.error.firstName'; } - if (values.lastName && !ValidationUtils.isValidName(values.lastName)) { + if (values.lastName && !ValidationUtils.isValidPersonName(values.lastName)) { errors.lastName = 'bankAccount.error.lastName'; } From e9a556e2d1eef9c14c262b27cc823cac439b3cca Mon Sep 17 00:00:00 2001 From: tienifr Date: Wed, 29 Nov 2023 10:59:45 +0700 Subject: [PATCH 6/6] add hyphenated test --- tests/unit/ValidationUtilsTest.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/ValidationUtilsTest.js b/tests/unit/ValidationUtilsTest.js index 57a67a2a740e..45de052f714d 100644 --- a/tests/unit/ValidationUtilsTest.js +++ b/tests/unit/ValidationUtilsTest.js @@ -318,6 +318,7 @@ describe('ValidationUtils', () => { test('Valid person name', () => { expect(ValidationUtils.isValidPersonName('test name')).toBe(true); expect(ValidationUtils.isValidPersonName(`X Æ A test`)).toBe(true); + expect(ValidationUtils.isValidPersonName(`a hyphenated-name`)).toBe(true); }); test('Invalid person name', () => {