All files / src/lib/validation/schemas product.ts

100% Statements 138/138
100% Branches 0/0
100% Functions 0/0
100% Lines 138/138

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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 1391x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * Product Validation Schemas
 *
 * Schemas for products, categories, reviews, and related operations.
 */
 
import { z } from 'zod';
import {
  idSchema,
  priceSchema,
  nonNegativeSchema,
  quantitySchema,
  shortTextSchema,
  longTextSchema,
  slugSchema,
  urlSchema,
  paginationSchema,
} from '../schemas';
 
// ============================================================================
// PRODUCT
// ============================================================================
 
/**
 * Create product schema
 */
export const createProductSchema = z.object({
  title: shortTextSchema,
  description: longTextSchema,
  price: priceSchema,
  discountPrice: priceSchema.optional(),
  sku: z.string().min(1).max(50).optional(),
  stock: nonNegativeSchema.optional().default(0),
  categoryId: idSchema,
  images: z.array(urlSchema).min(1, 'At least one image is required').max(10),
  featured: z.boolean().optional().default(false),
  status: z.enum(['DRAFT', 'ACTIVE', 'ARCHIVED']).optional().default('DRAFT'),
  metadata: z.record(z.string(), z.string()).optional(),
});
 
export type CreateProductInput = z.infer<typeof createProductSchema>;
 
/**
 * Update product schema (all fields optional)
 */
export const updateProductSchema = createProductSchema.partial();
 
export type UpdateProductInput = z.infer<typeof updateProductSchema>;
 
/**
 * Product query params schema
 */
export const productQuerySchema = paginationSchema.extend({
  categoryId: z.coerce.number().optional(),
  minPrice: z.coerce.number().optional(),
  maxPrice: z.coerce.number().optional(),
  inStock: z.coerce.boolean().optional(),
  featured: z.coerce.boolean().optional(),
  status: z.enum(['DRAFT', 'ACTIVE', 'ARCHIVED']).optional(),
  search: z.string().max(200).optional(),
});
 
export type ProductQueryInput = z.infer<typeof productQuerySchema>;
 
// ============================================================================
// CATEGORY
// ============================================================================
 
/**
 * Create category schema
 */
export const createCategorySchema = z.object({
  name: shortTextSchema,
  slug: slugSchema,
  description: longTextSchema.optional(),
  parentId: idSchema.optional(),
  image: urlSchema.optional(),
  displayOrder: z.number().int().default(0),
});
 
export type CreateCategoryInput = z.infer<typeof createCategorySchema>;
 
/**
 * Update category schema
 */
export const updateCategorySchema = createCategorySchema.partial();
 
export type UpdateCategoryInput = z.infer<typeof updateCategorySchema>;
 
// ============================================================================
// REVIEW
// ============================================================================
 
/**
 * Create review schema
 */
export const createReviewSchema = z.object({
  productId: idSchema,
  rating: z.number().int().min(1).max(5),
  comment: z.string().min(10, 'Review must be at least 10 characters').max(2000).optional(),
  title: z.string().max(100).optional(),
});
 
export type CreateReviewInput = z.infer<typeof createReviewSchema>;
 
/**
 * Update review schema
 */
export const updateReviewSchema = createReviewSchema.partial().omit({ productId: true });
 
export type UpdateReviewInput = z.infer<typeof updateReviewSchema>;
 
/**
 * Review query params
 */
export const reviewQuerySchema = paginationSchema.extend({
  productId: z.coerce.number().optional(),
  rating: z.coerce.number().min(1).max(5).optional(),
  verified: z.coerce.boolean().optional(),
});
 
export type ReviewQueryInput = z.infer<typeof reviewQuerySchema>;
 
// ============================================================================
// INVENTORY
// ============================================================================
 
/**
 * Inventory update schema
 */
export const updateInventorySchema = z.object({
  productId: idSchema,
  quantity: z.number().int(),
  reason: z.enum(['SALE', 'RETURN', 'ADJUSTMENT', 'RESTOCK', 'DAMAGE']),
  notes: z.string().max(500).optional(),
});
 
export type UpdateInventoryInput = z.infer<typeof updateInventorySchema>;