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])?)+$/; export function isValidEmail(email: string): boolean { if (!email || typeof email !== 'string') return false; if (email.length > 254) return false; return EMAIL_REGEX.test(email); } // The same rule the API applies to a checkout postal code (400 on `postalCode`), so the shopper is // told next to the field instead of by a failed request. Deliberately lenient: no per-country layout. // The phone has NO format rule here or in the API (only a 50 character ceiling): a shopper is never // blocked from buying over how a number is written ("03-1234567 ext 5"). /** Letters, digits, spaces and hyphens, at most 20 characters. */ export function isValidPostalCode(postalCode: string): boolean { return postalCode.length <= 20 && /^[\p{L}\p{N} -]*$/u.test(postalCode); } export type PasswordErrorCode = | 'passwordRequired' | 'passwordTooShort' | 'passwordNoUppercase' | 'passwordNoNumber' | 'passwordNoSymbol'; export function getPasswordError(password: string): PasswordErrorCode | null { if (!password || typeof password !== 'string') return 'passwordRequired'; if (password.length < 8) return 'passwordTooShort'; if (!/[A-Z]/.test(password)) return 'passwordNoUppercase'; if (!/[0-9]/.test(password)) return 'passwordNoNumber'; if (!/[^A-Za-z0-9]/.test(password)) return 'passwordNoSymbol'; return null; } const PASSWORD_ERROR_MESSAGES: Record = { passwordRequired: 'Password is required', passwordTooShort: 'Password must be at least 8 characters', passwordNoUppercase: 'Password must contain at least one uppercase letter', passwordNoNumber: 'Password must contain at least one number', passwordNoSymbol: 'Password must contain at least one special character', }; export function validatePassword(password: string): string | null { const code = getPasswordError(password); return code ? PASSWORD_ERROR_MESSAGES[code] : null; }