All files / actions/validators cart.js

90% Statements 9/10
100% Branches 0/0
100% Functions 2/2
90% Lines 9/10

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                  1x 1x                   1x 1x 2x 2x     2x 2x     1x        
import { z } from 'zod';
 
/* TODO:
- line item, add variation
- recipient
- promo
- add to cart (components)
- validate cart
 */
const cartLineItems = (lineItemList) => {
  const lineItemSchema = z.object({
    offeringId: z.coerce.string(),
    quantity: z.coerce.number(),
    deliveryMethod: z.string(),
    engravingMessage: z.array(z.string()).optional(),
    label: z.string()
  }).partial({
    label: true
  });
 
  const errors = [];
  const data = lineItemList.map((item) => {
    try {
      const validItem = lineItemSchema.parse(item, { stripUnknown: true });
      return validItem;
    } catch {
      errors.push(item);
      return item;
    }
  });
  return { data, errors };
};
 
export { cartLineItems };