-
-
Notifications
You must be signed in to change notification settings - Fork 189
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
Rule fired for optional attribute which is missing #540
Comments
This is helpful when dealing with optional keys. We can't just assume that we do not want to execute a rule when a value was missing. Good example: a password must be provided when login is provided, and both are optional. Refs #540
Please see #550 and let me know if this makes sense to you. |
This is helpful when dealing with optional keys. We can't just assume that we do not want to execute a rule when a value was missing. Good example: a password must be provided when login is provided, and both are optional. Refs #540
I'm gonna close this one since, after all, this is not a bug. See #550 where new features have been added to make it easier to work with optional keys and rules that depend on them. |
It is awesome, it does add the condition here and then more often than not but I do get the idea behind it so thank you, I really appreciate your work :) |
@mateusz-useo cool! I plan to add a bigger feature soon where you'll be able to use pattern matching to run rules conditionally, so ie this would work: rule(:numbers).if(:key?).each do
key.failure("must be even") if value.odd?
end |
for the record, this thing in PM is usually called "guard", just to save some time on coming with proper naming which is, you know, hard... |
Maybe |
@mateusz-useo oh that's a nice idea, thanks. I'll keep it in mind! |
Please consider how this can work with |
@shepmaster good point |
I've a contract like this: params do
optional(:per_page).filled(:integer)
.....
end
rule(:per_page).validate(gt?: 0, lteq?: 20)
..... If there's no I've been trying to upgrade dry-validation from 0.13 to >= 1.0.0. Optional keys are very common and this appears to be a major blocker for me to upgrade. We would love to see a solution implemented soon. Thank you. |
Exposing params do
optional(:per_page).filled(:integer)
end
rule(:per_page) do
validate(gt?: 0, lteq?: 20) if key?
end For params do
optional(:per_page).maybe(:integer)
end
rule(:per_page) do
validate(gt?: 0, lteq?: 20) if key? && value
end |
@musaffa I reckon it'd be simpler (and better UX too) to extend params do
optional(:per_page).maybe(:integer)
end
rule(:per_page).guard(:key?).validate(gt?: 0, lteq?: 20) that would just work and you could also use it with block-based rules. WDYT? |
A guard can be a good shortcut. It's simpler and concise.
I still think that the ability to call |
Yeah we can have probably # for optional
rule(:per_page).if(:key?).validate(gt?: 0, lteq?: 20)
# for maybe
rule(:per_page).unless(:nil?).validate(gt?: 0, lteq?: 20) We could even go nuts a bit and provide a shortcut (but I've got mixed feelings about this): rule(:per_page).if(:present?).validate(gt?: 0, lteq?: 20)
Yes this is a really good point, thank you. I'll experiment with this and get back to you. |
# for optional and maybe
rule(:per_page).if(:key?).if(:value?).validate(gt?: 0, lteq?: 20)
# for optional and maybe (combined if)
rule(:per_page).if(:key?, :value?).validate(gt?: 0, lteq?: 20)
I know where it's coming from. The spectre of ActiveModel validation haunts us all. |
@musaffa I reported two feature requests: It's tentatively scheduled to be part of 1.7.0 release. I marked both as |
Description
Rule is getting fired for a required attribute which is nested within an optional attribute and when the whole optional hash is missing (which should be allowed) it reports an error causing the validation to fail.
To Reproduce
Create a contract with a rule for an optional hash attribute with a required key + a rule for the required key and run with an input not including that hash. I use custom macro and I don't know if it's related to the bug.
So given:
And params:
It produces:
#<Dry::Validation::Result{:user=>{:id=>"d16dfef3-50f9-4588-b7d7-837801e403cd", :first_name=>"Blah", :last_name=>"Blah", :email=>"blah@blah.blah", :password=>"blahblahblah", :accepts_terms=>true, :clients=>[{:id=>"d287fdff-78ac-45ed-871b-b1b5c9548399"}]}} errors={:user=>{:freelancer=>{:id=>["not a valid UUID format"]}}}>
Expected behavior
The validation rule for optional attribute should never be run when it's missing.
In the example rules related to freelancer attributes should not be run and the validation should pass
My environment
The text was updated successfully, but these errors were encountered: