Skip to content

Instantly share code, notes, and snippets.

@codigoconjuan
Created July 9, 2025 19:40
Show Gist options
  • Save codigoconjuan/f7bc2f10689d34db21144f3a244e7140 to your computer and use it in GitHub Desktop.
Save codigoconjuan/f7bc2f10689d34db21144f3a244e7140 to your computer and use it in GitHub Desktop.
Action de Zod para validación de Productos
import { nullToEmptyString } from '@/utils'
import { z } from 'astro:schema'
const CommonFields = {
title: z.preprocess(
nullToEmptyString,
z.string().trim().min(1, { message: 'El Título no puede ir vacío' }),
),
featured_media: z.preprocess(
nullToEmptyString,
z.coerce.number().min(1, { message: 'La Imagen no puede ir vacia' }),
),
freshcoffee_category: z.number({ message: 'Categoría no válida' }).min(1, { message: 'Categoría no válida' }),
};
const FixedPriceSchema = z.object({
variable_price: z.literal('false'),
price: z.number({ message: 'El Precio no es válido' }),
...CommonFields,
});
const VariablePriceSchema = z.object({
variable_price: z.literal('true'),
small: z.number({ message: 'Precio chico requerido' }),
medium: z.number({ message: 'Precio mediano requerido' }),
large: z.number({ message: 'Precio grande requerido' }),
...CommonFields,
});
export const AddProductActionSchema = z.discriminatedUnion('variable_price', [
FixedPriceSchema,
VariablePriceSchema,
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment