All files / src/validation regex.ts

95.23% Statements 40/42
100% Branches 2/2
50% Functions 2/4
95.23% Lines 40/42

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 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                                            563x                     563x           563x                       563x                                       563x 60x                               563x           563x                               563x           563x                     563x                   563x                                 563x           563x                     563x                           563x                   563x                           563x                   563x                     563x                         563x                 563x                 563x                   563x                           563x                   563x                 563x                           563x                   563x                           563x                         563x                   563x                   563x                               563x                             563x                     563x 47x               563x                         563x       563x  
/**
 * Centralized Regular Expression Library
 *
 * This file contains validation regex patterns actively used across the application.
 * Centralizing these patterns ensures consistency, maintainability, and reusability.
 *
 * @module validation/regex
 */
 
// ============================================================================
// ZIP / POSTAL CODE PATTERNS
// ============================================================================
 
/**
 * US ZIP code validation pattern
 * Matches 5-digit ZIP codes or ZIP+4 format (with optional hyphen or space)
 *
 * @example "12345" ✓
 * @example "12345-6789" ✓
 * @example "12345 6789" ✓
 * @example "1234" ✗
 */
export const US_ZIP_REGEX = /^[0-9]{5}(?:[-\s]?[0-9]{4})?$/;
 
/**
 * International (non-US) ZIP/Postal code validation pattern
 * Supports various international postal code formats including alphanumeric characters
 *
 * @example "M5H 2N2" ✓ (Canada)
 * @example "SW1A 1AA" ✓ (UK)
 * @example "75008" ✓ (France)
 * @example "100-0001" ✓ (Japan)
 */
export const INTERNATIONAL_ZIP_REGEX = /^[a-zA-Z0-9 .,\-[\]]{3,10}$/;
 
/**
 * @deprecated Use US_ZIP_REGEX instead
 * Legacy export for backward compatibility
 */
export const zipRegex = US_ZIP_REGEX;
 
/**
 * Per-country postal code regex map.
 * Keys are ISO 3166-1 alpha-2 country codes (uppercase).
 *
 * To add a new country, add a single entry here — `buildZipSchema` picks it up automatically.
 *
 * @example CA: "A1A 1A1" or "A1A1A1"
 * @example GB: "SW1A 1AA", "M1 1AA", "EC1A 1BB"
 * @example DE/FR/ES: "75008", "10115", "28001"
 */
export const POSTAL_REGEX_BY_COUNTRY: Record<string, RegExp> = {
  /** United States: 12345 or 12345-6789 */
  US: US_ZIP_REGEX,
  /** Canada: A1A 1A1 or A1A1A1 */
  CA: /^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$/,
  /** United Kingdom: M1 1AA, SW1A 1AA, EC1A 1BB, etc. */
  GB: /^[A-Za-z]{1,2}\d[A-Za-z\d]?[ ]?\d[A-Za-z]{2}$/,
  /** Germany: 5-digit numeric (01067–99998) */
  DE: /^\d{5}$/,
  /** France: 5-digit numeric (01000–98799) */
  FR: /^\d{5}$/,
  /** Spain: 5-digit numeric (01000–52999) */
  ES: /^\d{5}$/,
};
 
/**
 * Returns the postal code regex for a given ISO 3166-1 alpha-2 country code,
 * or `null` if no country-specific pattern is registered.
 * Unrecognised countries fall back to `INTERNATIONAL_ZIP_REGEX`.
 */
export const getPostalRegexForCountry = (country: string): RegExp | null =>
  POSTAL_REGEX_BY_COUNTRY[country?.toUpperCase()] ?? null;
 
// ============================================================================
// EMAIL PATTERNS
// ============================================================================
 
/**
 * Email address validation pattern (RFC 5322 compliant)
 * Validates standard email addresses with common special characters
 *
 * @example "user@example.com" ✓
 * @example "first.last+tag@example.co.uk" ✓
 * @example "invalid@" ✗
 * @example "@invalid.com" ✗
 */
export const EMAIL_REGEX =
  /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
 
/**
 * @deprecated Use EMAIL_REGEX instead
 * Legacy export for backward compatibility
 */
export const isValidEmailRegex = EMAIL_REGEX;
 
// ============================================================================
// NAME PATTERNS
// ============================================================================
 
/**
 * Personal name validation pattern
 * Allows letters (including international characters), hyphens, apostrophes, spaces, and common punctuation
 *
 * @example "John Smith" ✓
 * @example "Mary-Jane O'Connor" ✓
 * @example "José García" ✓
 * @example "Anne-Marie Müller" ✓
 * @example "John123" ✗
 */
export const NAME_REGEX = /^[a-zA-ZÀ-ÖØ-öø-ÿĀ-žŽ-žßÐđŁłŒœÆæ'''`´\-–—.,\s]+$/;
 
/**
 * @deprecated Use NAME_REGEX instead
 * Legacy export for backward compatibility
 */
export const nameValidationRegex = NAME_REGEX;
 
/**
 * Business/Merchant name format validation
 * Ensures name contains alphanumeric characters and common business punctuation
 * Used in enterprise info, merchant info, and settings
 *
 * @example "ABC Corp." ✓
 * @example "Joe's Pizza" ✓
 * @example "Store #123" ✗
 */
export const BUSINESS_NAME_FORMAT_REGEX = /^[a-zA-Z0-9,.'\s]+$/;
 
/**
 * Business name minimum letters requirement
 * Ensures at least 3 alphabetic characters are present
 *
 * @example "ABC" ✓
 * @example "A1B2C3" ✓
 * @example "123" ✗
 */
export const BUSINESS_NAME_MIN_LETTERS_REGEX = /^(?=(.*[a-zA-Z]){3}).*$/;
 
// ============================================================================
// URL PATTERNS
// ============================================================================
 
/**
 * URL validation pattern (comprehensive)
 * Supports HTTP(S), FTP protocols, IPv4, domains, paths, query strings, and fragments
 * Includes international domain names (IDN) support
 *
 * @example "https://example.com" ✓
 * @example "http://subdomain.example.co.uk/path?query=1#fragment" ✓
 * @example "ftp://192.168.1.1:8080" ✓
 * @example "not-a-url" ✗
 */
export const URL_REGEX =
  /^((https?|ftp):)?\/\/(([a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF%!$&'()*+,;=:]+)@)?(((\d{1,3}\.){3}\d{1,3})|\[(\d{1,3}\.){3}\d{1,3}\]|([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(-[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*\.)+[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,})(:\d{2,5})?(\/[a-z\d\-._~%!$&'()*+,;=:@\/]*)?(\?[a-z\d\-._~%!$&'()*+,;=:@\/?]*)?(#[a-z\d\-._~%!$&'()*+,;=:@\/?]*)?$/i;
 
/**
 * @deprecated Use URL_REGEX instead
 * Legacy export for backward compatibility
 */
export const urlValidationRegex = URL_REGEX;
 
/**
 * URL without protocol validation
 * Ensures URL does not start with https://
 * Used in onboarding business details
 *
 * @example "example.com" ✓
 * @example "www.example.com" ✓
 * @example "https://example.com" ✗
 */
export const URL_WITHOUT_PROTOCOL_REGEX = /^(?!https:\/\/).*$/;
 
// ============================================================================
// BANKING PATTERNS
// ============================================================================
 
/**
 * Bank account number validation pattern
 * Validates US bank account numbers (6-17 digits)
 *
 * @example "123456" ✓
 * @example "12345678901234567" ✓
 * @example "12345" ✗
 */
export const BANK_ACCOUNT_NUMBER_REGEX = /^[0-9]{6,17}$/;
 
/**
 * Bank routing number validation pattern
 * Validates US bank routing numbers (9 digits)
 * Used in bank account creation and onboarding
 *
 * @example "123456789" ✓
 * @example "12345678" ✗
 */
export const ROUTING_NUMBER_REGEX = /^[0-9]{9}$/;
 
// ============================================================================
// ADDRESS PATTERNS
// ============================================================================
 
/**
 * Country code validation (ISO format, starts with uppercase letter)
 * Used in address schemas and business owner validation
 *
 * @example "US" ✓
 * @example "CA" ✓
 * @example "us" ✗
 */
export const COUNTRY_CODE_REGEX = /^[A-Z].+$/;
 
/**
 * State/Province validation (letters and spaces only)
 * Used in address and customer forms
 *
 * @example "California" ✓
 * @example "New York" ✓
 * @example "CA123" ✗
 */
export const STATE_REGEX = /^[a-zA-Z\s]*$/;
 
/**
 * Alphabetic text validation (letters and spaces only)
 * Used for text fields like occupation, employer, job titles
 * More semantically appropriate than STATE_REGEX for non-address fields
 *
 * @example "Software Engineer" ✓
 * @example "Google Inc" ✓
 * @example "123 Corp" ✗
 */
export const TEXT_ALPHABETIC_REGEX = /^[a-zA-Z\s]*$/;
 
// ============================================================================
// PASSWORD VALIDATION PATTERNS
// ============================================================================
 
/**
 * Password contains at least one number
 * Used in password strength validation
 *
 * @example "password123" ✓
 * @example "password" ✗
 */
export const PASSWORD_HAS_NUMBER_REGEX = /[0-9]/;
 
/**
 * Password contains at least one lowercase letter
 * Used in password strength validation
 *
 * @example "Password123" ✓
 * @example "PASSWORD123" ✗
 */
export const PASSWORD_HAS_LOWERCASE_REGEX = /[a-z]/;
 
/**
 * Password contains at least one uppercase letter
 * Used in password strength validation
 *
 * @example "Password123" ✓
 * @example "password123" ✗
 */
export const PASSWORD_HAS_UPPERCASE_REGEX = /[A-Z]/;
 
/**
 * Password contains at least one special character
 * Includes: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
 * Used in password strength validation
 *
 * @example "Password123!" ✓
 * @example "Password123" ✗
 */
export const PASSWORD_HAS_SPECIAL_CHAR_REGEX = /[!-/:-@[-`{-~]/;
 
// ============================================================================
// INPUT SANITIZATION PATTERNS
// ============================================================================
 
/**
 * Name input sanitization (default)
 * Removes special characters, digits, and leading/trailing spaces
 * Used in NameInput component for personal names
 *
 * @example Removes: ~`!@#$%^*&()_={}[]:;,.<>+?- and digits
 */
export const NAME_INPUT_SANITIZE_DEFAULT_REGEX =
  /[~`!@#$%^*&()_={}[\]:;,.<>+?-]+|\d+|^\s+$/g;
 
/**
 * Legal name input sanitization
 * Removes special characters and leading/trailing spaces (allows digits)
 * Used in legal name fields
 *
 * @example Removes: ~`!@#$%^*&()_={}[]:;<>+?-
 */
export const NAME_INPUT_SANITIZE_LEGAL_REGEX =
  /[~`!@#$%^*&()_={}[\]:;<>+?-]+|^\s+$/g;
 
/**
 * Merchant name input sanitization
 * Only allows alphanumeric, comma, period, apostrophe, and space
 * Used in merchant name fields
 *
 * @example Allows: a-z A-Z 0-9 , . ' space
 */
export const MERCHANT_NAME_SANITIZE_REGEX = /[^a-zA-Z0-9,.' ]/g;
 
// ============================================================================
// DOCUMENT IDENTIFICATION PATTERNS
// ============================================================================
 
/**
 * Passport ID validation
 * Alphanumeric, 6-9 characters
 * Used in identity verification forms
 *
 * @example "AB123456" ✓
 * @example "12345" ✗ (too short)
 */
export const PASSPORT_ID_REGEX = /^[A-Za-z0-9]{6,9}$/;
 
/**
 * National ID validation
 * Alphanumeric, 5-15 characters
 * Used in identity verification forms
 *
 * @example "A12345678" ✓
 * @example "1234" ✗ (too short)
 */
export const NATIONAL_ID_REGEX = /^[A-Za-z0-9]{5,15}$/;
 
// ============================================================================
// TIME AND DATE PATTERNS
// ============================================================================
 
/**
 * 12-hour time format validation (HH:MM AM/PM)
 * Used in event/sweepstake time validation
 *
 * @example "09:30 AM" ✓
 * @example "12:00 PM" ✓
 * @example "13:00 PM" ✗
 */
export const TIME_12HR_FORMAT_REGEX = /^(0[1-9]|1[0-2]):[0-5][0-9] (AM|PM)$/;
 
// ============================================================================
// NUMERIC VALIDATION PATTERNS
// ============================================================================
 
/**
 * Digits only validation
 * Matches one or more digits
 *
 * @example "123" ✓
 * @example "12a" ✗
 */
export const DIGITS_ONLY_REGEX = /^\d+$/;
 
/**
 * Integer validation (with optional negative sign)
 * Used for integer input fields
 *
 * @example "123" ✓
 * @example "-456" ✓
 * @example "12.5" ✗
 */
export const INTEGER_REGEX = /^-?[0-9]+$/;
 
/**
 * Optional digits validation
 * Allows empty string or digits
 *
 * @example "" ✓
 * @example "123" ✓
 * @example "12a" ✗
 */
export const OPTIONAL_DIGITS_REGEX = /^\d*$/;
 
// ============================================================================
// COLOR PATTERNS
// ============================================================================
 
/**
 * Hexadecimal color code validation
 * Supports 3 or 6 character hex codes with optional # prefix
 * Used in theme/branding color inputs
 *
 * @example "#FF0000" ✓
 * @example "#F00" ✓
 * @example "FF0000" ✓
 * @example "#GG0000" ✗
 */
export const HEX_COLOR_REGEX = /^#?([0-9A-F]{3}|[0-9A-F]{6})$/i;
 
// ============================================================================
// BUSINESS VALIDATION PATTERNS
// ============================================================================
 
/**
 * DBA (Doing Business As) name length validation
 * Ensures business name is between 3 and 21 characters
 * Used in merchant info forms
 *
 * @example "ABC" ✓ (3 chars)
 * @example "My Business Name LLC" ✓ (21 chars)
 * @example "AB" ✗ (too short)
 */
export const DBA_LENGTH_REGEX = /^.{3,21}$/;
 
// ============================================================================
// HELPER FUNCTIONS
// ============================================================================
 
/**
 * Test if a string is a valid email address
 * @param value - String to test
 * @returns true if valid email
 */
export const isValidEmail = (value: string): boolean => {
  return EMAIL_REGEX.test(value);
};
 
/**
 * Test if a string is a valid hex color code
 * @param value - String to test
 * @returns true if valid hex color
 */
export const isValidHexColor = (value: string): boolean => {
  return HEX_COLOR_REGEX.test(value);
};
 
/**
 * Test if a string contains only optional digits (empty string or digits only)
 * @param value - String to test
 * @returns true if value is empty or contains only digits
 * @example
 * isValidNumberValue("123") // true
 * isValidNumberValue("") // true
 * isValidNumberValue("12a3") // false
 */
export const isValidNumberValue = (value: string): boolean => {
  return OPTIONAL_DIGITS_REGEX.test(value);
};
 
export const CITY_MAX_LENGTH = 20;