react-hook-form / resolvers

📋 Validation resolvers: Yup, Zod, Superstruct, Joi, Vest, Class Validator, io-ts, Nope, computed-types, typanion, Ajv, TypeBox, ArkType, Valibot, effect-ts and VineJS
https://react-hook-form.com/
MIT License
1.72k stars 157 forks source link

feat: pass `options.names` as a second argument to a Vest suite #584

Closed vonagam closed 1 year ago

vonagam commented 1 year ago

React Hook Form has a way to signal to a resolver for which fields validations were requested (options.names/options.fields) and Vest has a way to skip unrequested validations (only/skip/include). But currently that names information gets discarded and Vest cannot utilize that functionality. I think it makes sense to pass names as a second argument.

Note: vestResolver has a second argument which is and never was utilized, other adapters do not have such thing. I think it should be put on note somewhere for removal in next major release.

jorisre commented 1 year ago

Thanks for the contribution. Can you add an unit test for this new option?

vonagam commented 1 year ago

Added a test with expected behavior.

jorisre commented 1 year ago

Thanks for the test.

The type should be updated as well: https://github.com/react-hook-form/resolvers/blob/master/vest/src/types.ts#L12

vonagam commented 1 year ago

The linked line is one that I made note of. It is unused and can be removed, but this is a breaking change, I assume.

There is no change in types, because all arguments are the same. Currently vest suite signature is undefined. It is simply ReturnType<typeof Vest.create> so can be any function (even though implementation expects that first argument is field values, it is not expressed in types currently).

vonagam commented 1 year ago

Ok, if it is allowed then it makes sense to type the expected call signature of a vest suite. I think this one is reasonable:

(values: TValues, names?: FieldName<TValues>[], context?: TContext) => void

So pass names as the second argument (such signature is shown many times in Vest documentation, mostly for passing to only) and a user supplied validation context as the third argument.

stale[bot] commented 1 year ago

Thank you for your contributions! This Pull Request has been automatically marked as stale because it has not had any recent activity. It will be closed if no further activity occurs. Best, RHF Team ❤️

vonagam commented 1 year ago

Any thoughts?

jorisre commented 1 year ago

I tried your code locally, everything seems to work as expected without any breaking changes. @bluebill1049 Any thoughts? I'm about to merge this PR

bluebill1049 commented 1 year ago

Sounds good to me 👍 @jorisre

bluebill1049 commented 1 year ago

There is a PR here as well: https://github.com/react-hook-form/react-hook-form/pull/10590

jorisre commented 1 year ago

FYI @bluebill1049 I merged but I'll release later with others fixes

github-actions[bot] commented 1 year ago

:tada: This PR is included in version 3.3.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: