Skip to content

Commit

Permalink
Merge pull request #31685 from tienifr/fix/31385
Browse files Browse the repository at this point in the history
Only allow alphabetic and latin characters for some fields in `CompanyStep`
  • Loading branch information
grgia authored Dec 5, 2023
2 parents 4aa5d8e + dadfc86 commit b1d0dba
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 47 deletions.
94 changes: 47 additions & 47 deletions src/libs/ValidationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ function validateCardNumber(value: string): boolean {
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)) {
return false;
}

return !CONST.REGEX.PO_BOX.test(value);
}

/**
* Validate date fields
*/
Expand Down Expand Up @@ -204,40 +193,6 @@ function isValidWebsite(url: string): boolean {
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;
Expand Down Expand Up @@ -304,6 +259,51 @@ function isValidPersonName(value: string) {
return /^[^\d^!#$%*=<>;{}"]+$/.test(value);
}

/**
* Validating that this is a valid address (PO boxes are not allowed)
*/
function isValidAddress(value: string): boolean {
if (!isValidLegalName(value)) {
return false;
}

return !CONST.REGEX.PO_BOX.test(value);
}

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;
}

/**
* Checks if the provided string includes any of the provided reserved words
*/
Expand Down Expand Up @@ -384,7 +384,6 @@ export {
meetsMinimumAgeRequirement,
meetsMaximumAgeRequirement,
getAgeRequirementError,
isValidAddress,
isValidDate,
isValidPastDate,
isValidSecurityCode,
Expand All @@ -396,7 +395,6 @@ export {
getFieldRequiredErrors,
isValidUSPhone,
isValidWebsite,
validateIdentity,
isValidTwoFactorCode,
isNumericWithSpecialChars,
isValidRoutingNumber,
Expand All @@ -409,6 +407,8 @@ export {
isValidValidateCode,
isValidDisplayName,
isValidLegalName,
isValidAddress,
validateIdentity,
doesContainReservedWord,
isNumeric,
isValidAccountRoute,
Expand Down
8 changes: 8 additions & 0 deletions src/pages/ReimbursementAccount/CompanyStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ function CompanyStep({reimbursementAccount, reimbursementAccountDraft, getDefaul
];
const errors = ValidationUtils.getFieldRequiredErrors(values, requiredFields);

if (values.companyName && !ValidationUtils.isValidLegalName(values.companyName)) {
errors.companyName = 'bankAccount.error.companyName';
}

if (values.addressStreet && !ValidationUtils.isValidAddress(values.addressStreet)) {
errors.addressStreet = 'bankAccount.error.addressStreet';
}
Expand All @@ -96,6 +100,10 @@ function CompanyStep({reimbursementAccount, reimbursementAccountDraft, getDefaul
errors.addressZipCode = 'bankAccount.error.zipCode';
}

if (values.addressCity && !ValidationUtils.isValidLegalName(values.addressCity)) {
errors.addressCity = 'bankAccount.error.addressCity';
}

if (values.companyPhone && !ValidationUtils.isValidUSPhone(values.companyPhone, true)) {
errors.companyPhone = 'bankAccount.error.phoneNumber';
}
Expand Down

0 comments on commit b1d0dba

Please sign in to comment.