Add shared form fields for contact details

This commit is contained in:
MatthieuTD
2025-09-25 14:44:42 +02:00
parent a4840c454f
commit 041478e9d4
11 changed files with 523 additions and 96 deletions

View File

@@ -0,0 +1,34 @@
import { normalizeEmail } from '~/utils/formatters/email'
export const EMAIL_INPUT_PATTERN = '[^\s@]+'
const EMAIL_VALIDATION_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
export type EmailValidationResult = {
valid: boolean
error?: string
}
export const EMAIL_VALIDATION_ERROR = 'Adresse email invalide'
/**
* Minimal email schema to align validation logic across the UI.
*/
export const emailSchema = {
pattern: EMAIL_VALIDATION_PATTERN,
message: EMAIL_VALIDATION_ERROR,
validate(value: string): EmailValidationResult {
const normalized = normalizeEmail(value)
if (!normalized) {
return { valid: true }
}
if (EMAIL_VALIDATION_PATTERN.test(normalized)) {
return { valid: true }
}
return { valid: false, error: EMAIL_VALIDATION_ERROR }
},
}
export const isEmailValid = (value: string): boolean => emailSchema.validate(value).valid

View File

@@ -0,0 +1,36 @@
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