"Expected number, received string" How do I update the values before the parsing validation. #163
Answered
by
IdoPesok
jericopulvera
asked this question in
Q&A
-
I'm trying to implement zsa and before I have this code. const validatedCheckoutData = await checkoutFormSchema.safeParse({
cartId: formData.get("cartId") as string,
items: formData.getAll("items[]"),
total: Number(formData.get("total")) || 0,
});
if (!validatedCheckoutData.success) {
console.error(
`Validation failed for checkout data: ${validatedCheckoutData.error.issues}`,
);
return {
errors: validatedCheckoutData.error.issues as ZodIssue[],
};
} export const checkoutFormSchema = z.object({
cartId: z.string(),
paymentMethod: paymentMethodSchema,
items: z.array(z.string()),
total: z.number().positive(),
}); <form
className="flex w-full flex-col gap-6 sm:max-w-xl"
onSubmit={async (event) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);
execute(formData);
}}
id="checkout-form"
>
<input
type="number"
name="total"
value={discountedTotal.getAmount()}
className="hidden"
/>
</form> |
Beta Was this translation helpful? Give feedback.
Answered by
IdoPesok
Jul 8, 2024
Replies: 1 comment
-
Hi, you can use export const checkoutFormSchema = z.object({
cartId: z.string(),
paymentMethod: paymentMethodSchema,
items: z.array(z.string()),
total: z.coerce.number().positive(),
}); For reference, check out coerce in the Zod docs |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
IdoPesok
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, you can use
coerce
. It will convert the string to a number before parsing.For reference, check out coerce in the Zod docs