-
-
Notifications
You must be signed in to change notification settings - Fork 503
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
Validator Request: required_if & required_unless #20
Comments
For Still, this is a nice idea to have such helpers anyway. Feel free to create a PR with such functionality if this is urgent. |
Today I came across the need for something like a In my case (using the same Laravel lingo) I was after a I implemented it like so: const { or, between, required } = require('vuelidate/lib/validators');
…
validations: {
field: {
betweenIfSet: or(between(1, 100), value => !required(value))
}
} Is this the recommended way to handle optional validation? By optional I mean if it's set, validate it, otherwise ignore? |
I think that’s a good approach. Actually, I think we can all work on creating such helpers and create pull requests for them. They won’t affect the bundle size so we can have really lots of them and people would be able to require just the ones they need. |
I believe this validator If you find it not true, please provide an example, as I consider it a bug. |
@dawiyo Imagine you have a checkbox to enable more form fields. We can easily create a function that grabs check state and tells if it's checked. To do it we can use function checkboxIsChecked () {
return this.checkboxState === true
} Later you can just use const requiredIfCheckbox = or(required, checkboxIsChecked) and in the end use it in your validation validations: {
myConditionalField: {
requiredIfCheckbox
}
} It effectively marks the field as valid in presence of checked checkbox, regardless of it's emptiness. Do you think it's enough for most cases, or any dedicated But here we go, example implementation: const requiredIf = otherProp => function (value) {
return this[otherProp] || required(value)
}
const requiredUnless = otherProp => function (value) {
return !this[otherProp] || required(value)
} usage, in pair with previous example: const requiredIfCheckbox = requiredIf('checkboxState') |
@Frizi Understood. I have no idea how I arrived at that position. Apologies, and thanks. |
@Frizi I appreciate the response and solution. I will say this though. I understand where you're coming from with the "it's not needed approach", however in my opinion, the beauty and genius of this library is its selective import approach. I (obviously) didn't come to the conclusion of using the |
@dawiyo I appreciate your input here, and i mostly agree, but there are a few things to add.
It's perfectly good approach to fill as many gaps as possible, as long as it's not making basic use cases any harder. It also means things harder to communicate are harder to use. Too many things in the docs add to the feeling of complexity and heaviness. Even if the library keeps easy things easy and weights only 1.5k in it's core! Another trait of this library is the easiness of using custom validators or even different libraries, as validator is simply a function. That way we don't have to add every single logical combination to the core and keep it maintained.
Agree again, and we do want to make use of it, and because of that i'm leaning towards accepting such thing. We have to keep in mind that browser bundle has all validator in a single file though, and there are people using it. Feel free to create a pull request with test coverage and documentation :) |
This is actually present in current release :) |
Would love to see
required_if
andrequired_unless
rules. Maybe something similar to Laravel's.The text was updated successfully, but these errors were encountered: