How to access general form refine errors from form state in a type-safe manner #776
Unanswered
bennettdams
asked this question in
Q&A
Replies: 1 comment 1 reply
-
I think you might need to update your useForm<UserUpdate & { general?: string; }> Does that help at all? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Update:
Sorry, this should've been posted in the React Hook Form repo, but one can't delete a discussion.
Original post:
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