All files / lib validation.ts

100% Statements 17/17
100% Branches 12/12
100% Functions 2/2
100% Lines 17/17

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                                    2x 29x 4x   25x 2x   23x 2x   21x 2x   19x 2x   17x           2x 25x 25x 6x   19x    
/**
 * Validation utilities for forms
 * @see /tests/unit/auth/validation.test.ts
 */
 
export interface PasswordValidationResult {
  valid: boolean;
  error: string | null;
}
 
/**
 * Validates password against Cognito requirements
 * - At least 8 characters
 * - One uppercase letter
 * - One lowercase letter
 * - One number
 * - One symbol
 */
export function validatePassword(password: string): PasswordValidationResult {
  if (password.length < 8) {
    return { valid: false, error: 'Password must be at least 8 characters' };
  }
  if (!/[A-Z]/.test(password)) {
    return { valid: false, error: 'Password must contain an uppercase letter' };
  }
  if (!/[a-z]/.test(password)) {
    return { valid: false, error: 'Password must contain a lowercase letter' };
  }
  if (!/[0-9]/.test(password)) {
    return { valid: false, error: 'Password must contain a number' };
  }
  if (!/[^A-Za-z0-9]/.test(password)) {
    return { valid: false, error: 'Password must contain a symbol (e.g., !@#$%^&*)' };
  }
  return { valid: true, error: null };
}
 
/**
 * Validates email format
 */
export function validateEmail(email: string): { valid: boolean; error: string | null } {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  if (!emailRegex.test(email)) {
    return { valid: false, error: 'Please enter a valid email address' };
  }
  return { valid: true, error: null };
}