All files / src/utils validation.ts

100% Statements 41/41
100% Branches 36/36
100% Functions 0/0
100% Lines 37/37

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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104            154x     14x   140x 13x 140x 1x 140x 1x         154x             155x     62x   93x 31x 93x 1x 93x 1x 93x 54x     155x                             19x       2x   17x 2x 17x 1x       19x   1x   18x 18x       19x   1x   18x 18x         1x         2x     19x          
export interface ValidationResult {
  isValid: boolean;
  errors: string[];
}
 
export const validateUsername = (username: string): ValidationResult => {
  const errors: string[] = [];
 
  if (!username || username.trim() === "") {
    errors.push("Username is required");
  } else {
    if (username.length < 3)
      errors.push("Username must be at least 3 characters");
    if (username.length > 50)
      errors.push("Username must be less than 50 characters");
    if (!/^[a-zA-Z0-9_]+$/.test(username))
      errors.push(
        "Username can only contain letters, numbers, and underscores"
      );
  }
 
  return {
    isValid: errors.length === 0,
    errors,
  };
};
 
export const validatePassword = (password: string): ValidationResult => {
  const errors: string[] = [];
 
  if (!password || password.trim() === "") {
    errors.push("Password is required");
  } else {
    if (password.length < 6)
      errors.push("Password must be at least 6 characters");
    if (password.length > 100)
      errors.push("Password must be less than 100 characters");
    if (!/[a-zA-Z]/.test(password))
      errors.push("Password must contain at least one letter");
    if (!/\d/.test(password))
      errors.push("Password must contain at least one number");
  }
 
  return {
    isValid: errors.length === 0,
    errors,
  };
};
 
export interface ProductFormData {
  name: string;
  price: number | string;
  quantity: number | string;
  description: string;
  category: string;
}
 
export const validateProduct = (product: ProductFormData): ValidationResult => {
  const errors: string[] = [];
 
  // Name validation
  if (!product.name || product.name.trim() === "") {
    errors.push("Product name is required");
  } else {
    if (product.name.length < 3)
      errors.push("Product name must be at least 3 characters");
    if (product.name.length > 100)
      errors.push("Product name must be less than 100 characters");
  }
 
  // Price validation
  const price = Number(product.price);
  if (isNaN(price)) {
    errors.push("Price must be a valid number");
  } else {
    if (price < 1) errors.push("Price must be greater than 0");
    if (price > 999999999) errors.push("Price must be less than 999,999,999");
  }
 
  // Quantity validation
  const quantity = Number(product.quantity);
  if (isNaN(quantity)) {
    errors.push("Quantity must be a valid number");
  } else {
    if (quantity < 0) errors.push("Quantity must be 0 or greater");
    if (quantity > 99999) errors.push("Quantity must be less than 99,999");
  }
 
  // Description validation
  if (product.description && product.description.length > 500) {
    errors.push("Description must be less than 500 characters");
  }
 
  // Category validation
  if (!product.category || product.category.trim() === "") {
    errors.push("Category is required");
  }
 
  return {
    isValid: errors.length === 0,
    errors,
  };
};