question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Skip validations if a field is not required

See original GitHub issue

Is your feature request related to a problem? Please describe. I have registered an optional text field with several validators. I think the validators should not run if the field is empty, because it’s optional, and an empty optional field is by definition valid.

The sandbox illustrates just one validation for simplicity.

Describe the solution you’d like If a field is empty and it was registered with required: false or without required specified, then skip running the validate functions.

Logical reasoning:

Here are all 6 combinations of a field (empty vs. non-empty) and required (true, false, undefined):

  • field empty
    • (1) required: true -> error already, no need to run validators
    • (2) required: false -> no error, no need to run validators because there’s nothing to validate
    • (3) required not specified -> no error, no need to run validators because there’s nothing to validate
  • field non-empty
    • (4) required: true -> no error yet, run validators for further validation
    • (5) required: false -> no error yet, run validators for further validation
    • (6) required not specified: no error yet, run validators for further validation

My proposal is to not run validators in cases (2) and (3). Validators should be run in all other cases.

The only exception I can think of for cases (2) and (3) is if the field has a validator that checks for content (minimum length, regexp match etc.), but that means the field should have been declared with { required: true } in the first place, which falls under cases (1) or (4), and validators should be run in those cases.

Describe alternatives you’ve considered

Currently, I have to prefix each validator to return true if the field value is empty:

validate: {
  rule1: value => value === '' || actualValidationLogic(value) || message;
  rule2(value) {
    if (value === '')
      return true;
    // ... actual validation logic
  }
  ...
}

This is redundant and inelegant.

Additional context

image

Benefits of adopting this proposal

  • Cleaner code. Validators no longer need to check for empty values.
  • Faster validation for empty non-required fields by skipping validation altogether.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:33
  • Comments:36 (23 by maintainers)

github_iconTop GitHub Comments

5reactions
risardi123commented, Sep 9, 2020

I got the solution @bluebill1049, I am realize that yup object is promise(), so if i place .min(), it’s still run the validation. So i use this solution like this. here is the codeSanbox link: https://codesandbox.io/s/boring-ramanujan-q3c54?file=/src/App.js

3reactions
abdurrahimkhcommented, Aug 9, 2022

Is this feature added ? i also want Skip validations if a field is not required

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Hook Form Skip validations if a field is not required
I think the validators should not run if the field is empty, because it's optional, and an empty optional field is by definition...
Read more >
How To: Skip, Bypass, or Disable required fields validation ...
On the Forms Layout tab, press the gear icon, set Backend validation to "No validation" · Save.
Read more >
How to skip required fields but not the custom validations of ...
I have a form which has a secondary button which is supposed to skip required fields but not the custom validation of fields...
Read more >
How can I skip field validation if the field is hidden?
The problem comes with the #requried = TRUE fields. When I submit the form it validates the two fields (enabled and disabled) but...
Read more >
Active Record Validations and Callbacks - Ruby on Rails Guides
Note that save also has the ability to skip validations if passed :validate ... not need to be recorded anywhere in your database...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found