All files / src/lib/core validators.ts

100% Statements 50/50
100% Branches 16/16
100% Functions 2/2
100% Lines 50/50

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 511x 1x 1x 15x 15x 15x 15x 15x 15x 15x 5x 5x 15x 4x 4x 15x 2x 2x 15x 15x 4x 15x 2x 2x 15x 15x 15x 15x 15x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 5x 5x 10x 5x 5x 10x 10x 10x 10x 10x  
/**
 * Validate product form data
 */
export function validateProductForm(data: Record<string, unknown>): {
  valid: boolean;
  errors: Record<string, string>;
} {
  const errors: Record<string, string> = {};
 
  if (!data.title || typeof data.title !== 'string' || !data.title.trim()) {
    errors.title = "Product title is required";
  }
  if (!data.price || parseFloat(String(data.price)) <= 0) {
    errors.price = "Valid price is required";
  }
  if (!data.categoryId) {
    errors.categoryId = "Category is required";
  }
  if (
    data.discountedPrice &&
    parseFloat(String(data.discountedPrice)) > parseFloat(String(data.price))
  ) {
    errors.discountedPrice = "Discounted price cannot be higher than price";
  }
 
  return {
    valid: Object.keys(errors).length === 0,
    errors};
}
 
/**
 * Validate category form data
 */
export function validateCategoryForm(data: Record<string, unknown>): {
  valid: boolean;
  errors: Record<string, string>;
} {
  const errors: Record<string, string> = {};
 
  if (!data.title || typeof data.title !== 'string' || !data.title.trim()) {
    errors.title = "Category title is required";
  }
  if (!data.slug || typeof data.slug !== 'string' || !data.slug.trim()) {
    errors.slug = "Category slug is required";
  }
 
  return {
    valid: Object.keys(errors).length === 0,
    errors};
}