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

Usage of bail() in validation schema #848

Closed
hamboomger opened this issue Mar 5, 2020 · 9 comments · Fixed by #851
Closed

Usage of bail() in validation schema #848

hamboomger opened this issue Mar 5, 2020 · 9 comments · Fixed by #851

Comments

@hamboomger
Copy link

Hello there!
Is there a way to get similar behaviour to what bait() method does using Schema Validation? The desired behaviour is to stop validation chain after a specific validation failed (exists in my case)

@ngocketit
Copy link

Like this:

  genres: {
    exists: {
      errorMessage: 'is required',
    },
    bail: true,
    isArray: true,
    custom: {
      options: (value: any[]) => {
        if (value.length <=0) return true
        return value.every(item => typeof item === 'string')
      }
    },
    errorMessage: 'must be array of string',
  },

However, there is no way to use multiple bail in schema. I think one solution is to have bail in options

@ngocketit
Copy link

This seems to work (e.g, error stops after exists) but if used with TypeScript, it won't be a valid type.

@hamboomger
Copy link
Author

hamboomger commented Mar 10, 2020

@ngocketit Yea, I actually use typescript in this project. What about making it a field inside the validation type, like this:

genres: {
    exists: {
      errorMessage: 'is required',
      bail: true,
    },
    isArray: {
      bail: true
    },
    custom: {
      options: (value: any[]) => {
        if (value.length <=0) return true
        return value.every(item => typeof item === 'string')
      }
    },
    errorMessage: 'must be array of string',
  },

I can try to implement this feature if you don't mind

@ngocketit
Copy link

@hamboomger That's very nice & it'd be awesome if it can be implemented.

@tommmn
Copy link
Contributor

tommmn commented Mar 10, 2020

I already have implemented it.
I will PR soon

@hamboomger
Copy link
Author

hamboomger commented Mar 10, 2020

@tommmn cool, thanks for mentioning it here! Should I close this issue or you will close it by PR?

This was referenced Mar 10, 2020
@tommmn
Copy link
Contributor

tommmn commented Mar 12, 2020

@hamboomger I've open PR. should wait for a merge if there are no problems.

@hamboomger
Copy link
Author

Oh you actually referenced this issue in your PR, sorry, didn't notice

@gustavohenke
Copy link
Member

Published in v6.9.0!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants