ZodZ
Zod2y ago
Alex

Alex - Hi there, I am trying to validate a numb...

Hi there, I am trying to validate a number using zod. The number is first received as a string and could possibly be anything because it comes from a user input.
z.coerce.number().int("Invalid Value").positive("Must be a positive number").safeParse(maxQuantity);
`
This is my current schema, where maxQuantity is the said string. Now when I pass "d" as maxQuantity, I receive the error "Must be a positive number". How do I give an error that this is not even a number?
Solution
z
            .string()
            .regex(/^[-]?\d*\.?\d+$/, "Must be a number")
            .pipe(z.coerce.number().int("Must be an integer").positive("Must be a positive number"))
            .safeParse(maxQuantity);
Was this page helpful?