import { normalizePhone } from '~/utils/formatters/phone' /** Pattern used for the HTML input `pattern` attribute on phone fields. */ export const PHONE_INPUT_PATTERN = '[0-9+ ]*' const PHONE_VALIDATION_PATTERN = /^\+?\d{7,15}$/ export type PhoneValidationResult = { valid: boolean error?: string } export const PHONE_VALIDATION_ERROR = 'Numéro de téléphone invalide' /** * Lightweight phone schema mirroring the API used across the app. */ export const phoneSchema = { pattern: PHONE_VALIDATION_PATTERN, message: PHONE_VALIDATION_ERROR, validate(value: string): PhoneValidationResult { const normalized = normalizePhone(value) if (!normalized) { // Empty values are considered valid; required-ness is handled by the caller. return { valid: true } } if (PHONE_VALIDATION_PATTERN.test(normalized)) { return { valid: true } } return { valid: false, error: PHONE_VALIDATION_ERROR } }, } export const isPhoneValid = (value: string): boolean => phoneSchema.validate(value).valid