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.
`
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:Jump to 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"))...
14 Replies
You have to split the string parsing and the number refinement into two steps.
Luckily with pipe, this is a little more elegant than the preprocess solution we had before.
Something like this:
https://codesandbox.io/p/devbox/pedantic-breeze-v4cc7w
cannot open the sandbox. what is that regex doing exactly? because I would expect my inital schema to give me an error because coerce should fail. also, because I get "Must be a positive number" as error, that means the int check was passed, which again means, coerce returned a number. that should not be the case, because coerce just uses the Number() constructor, which would give NaN when passing "d"
Coerce just literally calls
Number(val)
which doesn't fail, just returns NaN
correct, so how is the int check passing?
That regex just ensures that the string is an integer-like thing
I'm not sure, to be honest.
I am also very confused, I will investigate further, just wanted to check I am not doing something obvious wrong
fwiw, when I try to run it in the codesandbox, I do get the error for
int
with "d"
as the input
oh, actually I get expected number received nan as the error.
for some reason I thought number
accepted nan
but maybe that's changed since the last time I looked at itmaybe I am just reading one error for some reason, i will check
nope, but still cannot access the code sandbox
Ahh, here ya go: https://codesandbox.io/p/devbox/pedantic-breeze-v4cc7w?file=%2Fsrc%2Findex.ts%3A11%2C40
Wasn't up to speed on all the latest codesandbox changes to sharing and permissions.
yes, works, thanks
now using this, which works as I originally intended:
Might want to tweak the regex to allow negative numbers so that you don't reject
"-100"
with "Must be a number", but yeah that seems about right!yes, true, thanks!
Solution
Thanks again, very helpfull!