All files / src/pages/inventory/validation inventoryValidation.ts

100% Statements 96/96
100% Branches 2/2
100% Functions 0/0
100% Lines 96/96

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 971x 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  
/**
 * @file inventoryValidation.ts
 * @module pages/inventory/validation/inventoryValidation
 *
 * @summary
 * Centralized validation schemas for inventory management forms.
 * Zod schemas for inventory create/edit, quantity adjustment, and price change.
 *
 * Uses Zod for type-safe validation with custom error messages.
 *
 * @enterprise
 * - Uses z.coerce.number() so inputs can be strings (from text fields) or numbers.
 * - No any. Clear, field-level error messages suitable for form mapping.
 */
 
import { z } from 'zod';
 
/**
 * Schema for creating or updating inventory items.
 * Handles both new item creation and existing item updates.
 */
export const itemFormSchema = z.object({
  name: z.string().min(1, "Item name is required"),
  code: z.string().optional(),
  supplierId: z.union([z.string(), z.number()]).refine(val => val !== "" && val !== 0, "Supplier is required"),
  quantity: z.number().min(0, "Initial stock must be non-negative"),
  price: z.number().min(0, "Price must be non-negative"),
  reason: z.enum(["INITIAL_STOCK", "MANUAL_UPDATE"], {
    message: "Reason is required",
  }),
});
 
export type UpsertItemForm = z.infer<typeof itemFormSchema>;
 
/**
 * Schema for adjusting item quantities.
 * Used in quantity adjustment dialogs.
 */
export const quantityAdjustSchema = z.object({
  itemId: z.string().min(1, 'Item selection is required'),
  newQuantity: z.number().min(0, 'Quantity cannot be negative'),
  reason: z.string().min(1, 'Reason is required'),
});
 
export type QuantityAdjustForm = z.infer<typeof quantityAdjustSchema>;
 
/**
 * Schema for changing item prices.
 * Used in price change dialogs.
 * 
 * @validation
 * - itemId: Required, identifies which item to update
 * - newPrice: Must be positive (> 0), backend validates this as well
 * 
 * @note Backend does not require a "reason" parameter for price changes
 */
export const priceChangeSchema = z.object({
  itemId: z.string().min(1, 'Item selection is required'),
  newPrice: z.number().positive('Price must be greater than 0'),
});
 
export type PriceChangeForm = z.infer<typeof priceChangeSchema>;
 
/**
 * Schema for editing item names.
 * Used in edit item dialogs.
 * 
 * @validation
 * - itemId: Required, identifies which item to update
 * - newName: Must not be empty, should be different from current name
 * 
 * @note Backend validates that the new name is not a duplicate for the same supplier
 * @note Only ADMIN users can change item names
 */
export const editItemSchema = z.object({
  itemId: z.string().min(1, 'Item selection is required'),
  newName: z.string().min(1, 'Item name is required'),
});
 
export type EditItemForm = z.infer<typeof editItemSchema>;
 
/**
 * Schema for deleting inventory items.
 * Used in delete item dialogs.
 * 
 * @validation
 * - itemId: Required, identifies which item to delete
 * 
 * @note Backend validates that item quantity is 0 before allowing deletion
 * @note Only ADMIN users can delete items
 */
export const deleteItemSchema = z.object({
  itemId: z.string().min(1, 'Item selection is required'),
});
 
export type DeleteItemForm = z.infer<typeof deleteItemSchema>;