Ensure uniqueness for zod schema field
Hello!
I am using
react-hook-form
and have the following zod object to validate the input participants:
How can I validate using Zod that each email
is unique, aswell as each name
? Something like a .unique()
is what I am looking for 🙂
Thank you!8 Replies
I think you need to write a custom schema https://zod.dev/?id=custom-schemas
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
Yeah you might be right! Not sure though, since the custom schema would have to evaluate itself, kind of a catch 22 situation 🤔
I might just do the error handling separately on form submission instead 🤷♂️
ill try making it with zod
actually refine might be what we're looking for https://zod.dev/?id=refine
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
Yes! It should be useable on the participants array
this works
if you want more specific details like "names must be unique" and "emails must be unique" you can trivially chain another
.refine
and move one of them to the other
i tested with
Thank you, this works great!
🫡
fix the error messages back to what you had