dry-validation
dry-validation copied to clipboard
Add key.success? and key.failure? predicates
In complex rules, it may be useful to know whether the key has any failure message already.
rule(:foos).each do
key.success? # => true
key.failure? # => false
key.failure('wrong')
key.success? # => false
key.failure? # => true
end
If it's ok, I'll give a try to it.
Sure.
Hmmm this seems tricky... it implies changing the architecture so that the evaluator and failures for every key are shared between all rules. Am I wrong?
For me, it would be enough to work only within a single rule run. Though I can see how it can be not immediately obvious. @solnic any thoughts?
rule.failure?
or something along the lines could be another approach
Probably rule.failure?
makes more sense, as key.failure?
implies that key(name).failure?
should work. However, the scenario where evaluator is shared also makes sense for even more complex rules :)
I forgot we have error?
. Currently it expects path arg but we can set it to the default key, so you could have:
rule(:foo) do
error? # expands to `error?("foo")`
error?("other.path") # if you need to check something else
end
I think this should be enough and we don't need key.failure/success?
.
works for me though rule.error?
/rule.failure?
still could be useful
@flash-gordon why?
@solnic rules can be quite convoluted, especially in macros. This is the exact need I had when filed this issue. Keeping them simple is not always feasible.
@flash-gordon ok makes sense
@waiting-for-dev so I think adding a default argument to error?
would be enough here. I can add a separate feature request about rule.error?
.