resolvers: ZodResolver error: "Expected number, received string"
The zodResolver example does not work as there is alway the following error.
{
"age":{
"message":"Expected number, received string",
"type":"invalid_type"
}
}
Here is a code sandbox to reproduce the error: https://codesandbox.io/s/react-hook-form-zod-resolver-example-forked-0p8rt?file=/src/App.js
Thanks for your reply/help.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 12
- Comments: 24 (3 by maintainers)
Commits related to this issue
- [common] contract.ts - avoid zod validation failures on valid IDs Have been seeing errors when processing requests with valid numeric IDs in the search endpoint, along the lines of ts-rest / zod reje... — committed to DigitalCommons/mykomap-monolith by deleted user 2 years ago
- [common] contract.ts - avoid zod validation failures on valid IDs Have been seeing errors when processing requests with valid numeric IDs in the search endpoint, along the lines of ts-rest / zod reje... — committed to DigitalCommons/mykomap-monolith by deleted user 2 years ago
- [common] contract.ts - avoid zod validation failures on valid IDs Have been seeing errors when processing requests with valid numeric IDs in the search endpoint, along the lines of ts-rest / zod reje... — committed to DigitalCommons/mykomap-monolith by deleted user 2 years ago
- [common] contract.ts - avoid zod validation failures on valid IDs Have been seeing errors when processing requests with valid numeric IDs in the search endpoint, along the lines of ts-rest / zod reje... — committed to DigitalCommons/mykomap-monolith by deleted user 2 years ago
- [common] contract.ts - avoid zod validation failures on valid IDs Have been seeing errors when processing requests with valid numeric IDs in the search endpoint, along the lines of ts-rest / zod reje... — committed to DigitalCommons/mykomap-monolith by deleted user 2 years ago
- [common] contract.ts - avoid zod validation failures on valid IDs Have been seeing errors when processing requests with valid numeric IDs in the search endpoint, along the lines of ts-rest / zod reje... — committed to DigitalCommons/mykomap-monolith by deleted user 2 years ago
Coerce primitive values with Zod. Full example
After a bit of digging around, i’ve discovered that the following achieves what i was looking for:
@lailo If you won’t update to
zod@betav2 and don’t need to transform the value in number, you can do the following:Please see coercion: https://github.com/colinhacks/zod#coercion-for-primitives
Hi @lailo , this is “normal” when using an input
type="number",e.target.valueis a string, to have a number, you should usee.target.valueAsNumber. You can find an answer here https://github.com/react-hook-form/resolvers/issues/62.You’re right, the example does not work as expected, we have to update it 😃
This worked perfect for me, many thanks @omgismartinez
You saved my time. Thanks!
Solved the issue. Thanks
works, perfectly!
Thanks @adamwdennis worked for me
@adamwdennis It works but not if input is optional. When submit is done with empty input i get “Expected number, received nan”
Here is a better fix :
with
lol you already got the reply 😂
thats working for me. thanks!
Input like “123aaa” bypasses validation due to parseInt ignoring non-numeric characters after a valid number.