All files / utils validateForms.ts

100% Statements 35/35
100% Branches 25/25
100% Functions 1/1
100% Lines 33/33

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                                            2x 27x   27x 8x 1x   7x 7x       27x 12x 1x   11x 2x   9x 9x 4x           27x 8x 2x 6x 1x       27x 8x 1x 7x 2x       27x 9x 1x   8x 8x       27x 2x     27x    
/**
 * Type représentant les données partielles d'un formulaire client
 */
export type PartialFormData = Partial<{
    email: string;
    password: string;
    nom: string;
    prenom: string;
    telephone: string;
    acceptTerms: boolean;
}>;
 
/**
 * Type représentant les erreurs de validation d'un formulaire
 */
export type Errors = Partial<Record<keyof PartialFormData, string>>;
 
/**
 * Valide les données d'un formulaire client
 * @param formData - Les données du formulaire à valider
 * @returns Un objet contenant les erreurs de validation
 */
export function validateClientForm(formData: PartialFormData): Errors {
    const errors: Errors = {};
 
    if ('email' in formData) {
        if (!formData.email) {
            errors.email = "Email obligatoire.";
        } else {
            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            if (!emailRegex.test(formData.email)) errors.email = "Email invalide.";
        }
    }
 
    if ('password' in formData) {
        if (!formData.password) {
            errors.password = "Mot de passe obligatoire.";
        } else {
        if (formData.password.length < 12) {
            errors.password = "Le mot de passe doit contenir au moins 12 caractères.";
        } else {
            const strongPassRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).+$/;
            if (!strongPassRegex.test(formData.password)) {
                errors.password = "Le mot de passe doit contenir au moins une majuscule, une minuscule, un chiffre et un caractère spécial.";
            }
        }
        }
    }
 
    if ('nom' in formData) {
        if (!formData.nom) {
            errors.nom = "Nom obligatoire.";
        } else if (formData.nom.length < 2) {
            errors.nom = "Nom trop court.";
        }
    }
 
    if ('prenom' in formData) {
        if (!formData.prenom) {
            errors.prenom = "Prénom obligatoire.";
        } else if (formData.prenom.length < 2) {
            errors.prenom = "Prénom trop court.";
        }
    }
 
    if ('telephone' in formData) {
        if (!formData.telephone) {
            errors.telephone = "Téléphone obligatoire.";
        } else {
        const phoneRegex = /^(\+33|0)[1-9](\d{2}){4}$/;
        if (!phoneRegex.test(formData.telephone)) errors.telephone = "Téléphone invalide.";
        }
    }
 
    if ('acceptTerms' in formData && formData.acceptTerms === false) {
        errors.acceptTerms = "Vous devez accepter les CGU et la politique de confidentialité.";
    }
 
    return errors;
}