How to access Zod's general form refine errors from form state in a type-safe manner #7111
Replies: 8 comments 4 replies
-
Hello, I'm facing the same issue, did you find any solution to this? |
Beta Was this translation helpful? Give feedback.
-
It have been 3 years, i haven't found any solution for this. Sadge. |
Beta Was this translation helpful? Give feedback.
-
I found the answer!
To:
And now you can unwrap the form and use the
|
Beta Was this translation helpful? Give feedback.
-
Unfortunately, it does not work anynore. |
Beta Was this translation helpful? Give feedback.
-
i have the same problem |
Beta Was this translation helpful? Give feedback.
-
Hi, I use this solution to display global error message :
First, I create an optional field (confirmTag in the previous code) just to display the global error and I link my global validation to this link with the path parameter. Finally, I create a FormField based on the field confirmTag just to display global error (I use Shadcn/UI but we can follow the same logic just with react-hook-form)
I used this post to find this solution. I don't know if it's the best way to do that but it works good, it's fast to do and robust. |
Beta Was this translation helpful? Give feedback.
-
I also have this problem. @hocino's solution works (thank you for sharing) but it's more a workaround that a real solution to the issue. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to use this with zod4 and the general |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using general refine errors in my schema, but I don't know how to access them in a type-safe manner.
I have a simple schema that I want to use to update a user.
The user can either
a) input a new username
b) input a new password
c) input a new username & a new password
This means that
username
andpassword
are both optional, hence thepartial
.That's why I additionally have
refine
to check whether either a new username or a new password has been given as input,because I don't want to submit if both are empty.
As you can see, the
refine
error is not used specifically forusername
orpassword
, it's rather a general "form" error. I created a new keygeneral
for that.Now, when I want to access this error, Zod does not know it:
Property 'general' does not exist on type '{ username?: FieldError | undefined; password?: FieldError | undefined; }'.ts(2339)
This works at runtime, the
refine
errors are added correctly, but TypeScript is mad at me.Can I somehow while creating the schema let Zod know how my custom error field is called, without a custom
ZodErrorMap
?Or is there maybe an easier way to handle these general errors altogether?
Beta Was this translation helpful? Give feedback.
All reactions