r/reactjs • • Dec 17 '23

Discussion Is react hook form overhyped?

I created a custom form validation hook.Its a simple hook and I felt I didn't needed react hook forms for just simple form validation. For simpe validation writing that much code and loading the whole library felt not right. Maybe I am wrong.

0 Upvotes

45 comments sorted by

View all comments

Show parent comments

3

u/svish Dec 17 '23

Actually, for forms superRefine it really terrible.

First of all, you get your validation very awkwardly cut in half, or worse. If B depends on A, you can only validate B partially, for example having to keep it optional even if its required when A is true. Even worse when there's a series of dependencies.

Second, and worst of all, zod doesn't even touch the refinements until the whole "base object" is valid. This is fine in most cases, but for forms this means the user doesn't get certain error messages until they've gone through the whole form. Get to the end, and suddenly a whole new "layer" of errors pop up.

For forms, Zod is just awful in every way that matters the most.

1

u/ashenzo Dec 18 '23 edited Dec 18 '23

If you define the fields that relate to each other in their own schema and refine the validation there, then intersect them with another schema containing the rest of the form, the refine checks will run on the first validation pass with the rest of it. This is super hacky and annoying but it does work.

Note: you can only intersect 2 schemas at a time for this to work, but you can intersect more than two schemas multiple times (2 each intersection).

2

u/svish Dec 18 '23

Do you realize how many steps and complicated words you just used? That is not just "hacky", it's awful :p

yup.number().when(
  ['x', 'y'],
  ([x, y], schema) => x === true && typeof y === 'number'
    ? schema.required().max(y)
    : schema
)

1

u/ashenzo Dec 18 '23

I know, it sucks!! Just posting it as an fyi - took me ages to figure that out 😅

I had also made the decision to use Yup the next time I need to work on a big form because of that same issue.

It looked like zod maintainers had no intention of fixing the above when I last looked into it, which is really bizarre.

1

u/svish Dec 18 '23

It's a bit annoying, but I also get it because one of zod main focus is typescript and strict typing. Simply the addition of a when kind of feature complicates the types quite a lot...