Add shared form fields for contact details
This commit is contained in:
34
app/shared/validation/email.ts
Normal file
34
app/shared/validation/email.ts
Normal 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
|
||||
Reference in New Issue
Block a user