Replies: 2 comments 4 replies
-
Interesting question. I look at it this way in my own apps:
If you are satisfied with no/yes/yes answers to the above questions, you probably don't need to do the "double validation" thing. If not (and absolutely not if you have API routes that call the same server actions), you really will want to validate them at the server level. I would tend to implement this in the API route handler itself (before the server action is called), symmetric with the way we ask RHF to do a client side validation before the server action is ever called. But that's more doing something that fits best in your overall architecture than a hard and fast rule. If you decide you do need double validation, you should also explore factoring your Zod schemas (or whatever) into separate modules that can be reused on both sides, to reduce code duplication problems. |
Beta Was this translation helpful? Give feedback.
-
I've found this video this morning that I believe now completely solves my issue. It uses the validation from Zod to do input validation so I don't need to duplicate my validation anyplace as long as I pull out my zod schema into a sharable asset. It also does require using the @hookform/resolver/zod package. https://youtu.be/FXWD_etMJWA?list=PLeO8M-2wYaaV5vh2lRWV7qt_-Io8agaf-&t=2421 |
Beta Was this translation helpful? Give feedback.
-
I'm new to react-hook-form and am a little confused on when (and why) to use Zod and how that integrates with Server Actions (nextJS 14). I had that that Zod does validation to insure that the data you POST to the server action is in a correct format. What confuses me is that I thought that is what react-hook-form does so why do it twice (and how to make sure they are always doing exactly the same validation?).
Also, it makes sense to me to verify the payload with zod in the Server Action. Assuming I got that right, is there a way when using react-hook-form not to have to define the schema and the validation rules separately on the client, and then again on the server?
Sorry if I missed the NextJS 14 example. I looked but did not find one,
Thanks, -Peter Kellner
Beta Was this translation helpful? Give feedback.
All reactions