Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v4] useField(), chain of GenericValidateFunction and bails #3725

Closed
gbaquedano opened this issue Mar 12, 2022 · 0 comments · Fixed by #3726
Closed

[v4] useField(), chain of GenericValidateFunction and bails #3725

gbaquedano opened this issue Mar 12, 2022 · 0 comments · Fixed by #3726
Labels
✨ enhancement a "nice to have" feature

Comments

@gbaquedano
Copy link
Contributor

gbaquedano commented Mar 12, 2022

Is your feature request related to a problem? Please describe.
Let's imagine I'm developing a component with a field in which, for whatever reason, I can't use global rules. The component has the rules locally defined or are passed as props. Let's assume the component should validate more than one rule but it also needs expose all the errors from this validation if bails is false.

I know I can define my own function to chain this non global rules and return an error message as a rule fails.
Is it possible to set more than one error and respect bails behavior inside a GenericValidateFunction?

Describe the solution you'd like
Accepting a chain of validation functions as rules:

const {
  value: value,
  errors: errors,
} = useField(
  'field',
  [
    val => (val ? true : REQUIRED_MESSAGE),
    val => ((val as string)?.length >= 3 ? true : MIN_MESSAGE)
  ],
  { bails: false }
);

In this case, if value is not set and I run validation I would expect errors to be:

[ REQUIRED_MESSAGE, MIN_MESSAGE ]

If I set bails to true, validation would stop on the first rule that fails, for the same example errors would be:

[ REQUIRED_MESSAGE ]

The order of evaluation of the rules follows the order of the specified chain.

Describe alternatives you've considered
I currently can bypass this issue using global rules. Not sure about validation order though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement a "nice to have" feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants