All files / src/hooks useSignupValidation.ts

100% Statements 8/8
100% Branches 6/6
100% Functions 3/3
100% Lines 6/6

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                                                6x 6x 6x   6x 6x   6x                
import { useMemo } from 'react';
import { isStrongPassword, isEmailValid } from '../utils/validation';
 
export interface SignupValidationParams {
  firstname: string;
  lastname: string;
  email: string;
  password: string;
  confirmPassword: string;
  firstnameTouched: boolean;
  lastnameTouched: boolean;
  emailTouched: boolean;
}
 
export function useSignupValidation({
  firstname,
  lastname,
  email,
  password,
  confirmPassword,
  firstnameTouched,
  lastnameTouched,
  emailTouched,
}: SignupValidationParams) {
  const firstnameError = firstnameTouched && firstname.trim() === '';
  const lastnameError  = lastnameTouched  && lastname.trim() === '';
  const emailError     = emailTouched     && !isEmailValid(email);
 
  const pwStrong  = useMemo(() => isStrongPassword(password), [password]);
  const pwsMatch  = useMemo(() => password === confirmPassword, [password, confirmPassword]);
 
  return {
    firstnameError, lastnameError, emailError,
    pwStrong, pwsMatch,
    isFirstnameValid: firstname.trim() !== '',
    isLastnameValid:  lastname.trim()  !== '',
    isEmailValid:     isEmailValid(email),
  };
}