Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 92x 29x 29x 29x 7x 22x 9x 13x 29x 29x 92x 1536x 92x 4x 2x 2x 2x 92x | /**
* Validation Utility Functions
*
* Reusable validation utilities for forms and inputs
*
* @module validation/utils
*/
import * as Yup from "yup";
import {
NAME_INPUT_SANITIZE_DEFAULT_REGEX,
NAME_INPUT_SANITIZE_LEGAL_REGEX,
MERCHANT_NAME_SANITIZE_REGEX,
PASSWORD_HAS_NUMBER_REGEX,
PASSWORD_HAS_LOWERCASE_REGEX,
PASSWORD_HAS_UPPERCASE_REGEX,
PASSWORD_HAS_SPECIAL_CHAR_REGEX,
} from "./regex";
/**
* Name Input Normalization Options
*/
export interface NameNormalizationOptions {
isLegalName?: boolean;
isMerchantName?: boolean;
}
/**
* Normalizes name input by removing forbidden characters and extra whitespace
*
* @param value - The input value to normalize
* @param options - Normalization options (isLegalName, isMerchantName)
* @returns Normalized string value
*
* @example
* ```typescript
* const normalized = normalizeNameInput("John@@Doe ", { isLegalName: false });
* // Returns: "JohnDoe "
* ```
*/
export const normalizeNameInput = (
value: string,
options: NameNormalizationOptions = {},
): string => {
Iif (!value) return value;
const { isLegalName, isMerchantName } = options;
let charsRegex: RegExp;
if (isLegalName) {
charsRegex = NAME_INPUT_SANITIZE_LEGAL_REGEX;
} else if (isMerchantName) {
charsRegex = MERCHANT_NAME_SANITIZE_REGEX;
} else {
charsRegex = NAME_INPUT_SANITIZE_DEFAULT_REGEX;
}
const currentValue = value.replace(charsRegex, "").replace(/\s+/g, " ");
return currentValue;
};
/**
* Adds password validation rules to an existing Yup schema
*
* @param schema - Existing Yup string schema to enhance
* @returns Enhanced schema with password validation rules
*
* @example
* ```typescript
* const schema = addPasswordValidationRules(
* Yup.string().required("Password is required")
* );
* ```
*/
export const addPasswordValidationRules = (
schema: Yup.StringSchema,
): Yup.StringSchema => {
return schema
.matches(
PASSWORD_HAS_NUMBER_REGEX,
"Password must contain at least one number",
)
.matches(
PASSWORD_HAS_LOWERCASE_REGEX,
"Must contain at least one lowercase character",
)
.matches(
PASSWORD_HAS_UPPERCASE_REGEX,
"Must contain at least one uppercase character",
)
.matches(
PASSWORD_HAS_SPECIAL_CHAR_REGEX,
"Must contain at least one special character",
);
};
// ============================================================================
// BUSINESS OWNERSHIP UTILITIES
// ============================================================================
/**
* Refines business ownership type based on legal entity type.
* Used in LE (Legal Entity) forms.
*
* @param businessType - The type of business entity
* @param ownershipType - Optional explicit ownership type
* @returns Refined ownership type ("public" or "private")
*
* @example
* ```typescript
* refineLEOwnership("individual_sole_proprietorship"); // Returns: "private"
* refineLEOwnership("tax_exempt_organization"); // Returns: "public"
* refineLEOwnership("corporation", "private"); // Returns: "private"
* refineLEOwnership("corporation"); // Returns: "public" (default)
* ```
*/
export const refineLEOwnership = (
businessType: string,
ownershipType?: string,
): string => {
// Individual sole proprietorships are always private
if (businessType === "individual_sole_proprietorship") {
return "private";
}
// Tax exempt and government entities are always public
Iif (
businessType === "tax_exempt_organization" ||
businessType === "government_agency"
) {
return "public";
}
// For other entity types, use provided ownershipType or default to "public"
return ownershipType || "public";
};
/**
* Refines business ownership type in onboarding flow.
* Differs from refineLEOwnership by NOT forcing individual_sole_proprietorship to private.
*
* @param businessType - The type of business entity
* @param ownershipType - Optional explicit ownership type
* @returns Refined ownership type ("public" or "private")
*
* @example
* ```typescript
* refineBusinessOwnershipInOnboarding("tax_exempt_organization"); // Returns: "public"
* refineBusinessOwnershipInOnboarding("individual_sole_proprietorship", "private"); // Returns: "private"
* refineBusinessOwnershipInOnboarding("corporation"); // Returns: "public" (default)
* ```
*/
export const refineBusinessOwnershipInOnboarding = (
businessType: string,
ownershipType?: string,
): string => {
// Tax exempt and government entities are always public
if (
businessType === "tax_exempt_organization" ||
businessType === "government_agency"
) {
return "public";
}
// For all other entity types (including individual_sole_proprietorship),
// use provided ownershipType or default to "public"
return ownershipType || "public";
};
|