Created
July 9, 2025 19:40
-
-
Save codigoconjuan/f7bc2f10689d34db21144f3a244e7140 to your computer and use it in GitHub Desktop.
Action de Zod para validación de Productos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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