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

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...