dry-validation icon indicating copy to clipboard operation
dry-validation copied to clipboard

Add key.success? and key.failure? predicates

Open flash-gordon opened this issue 4 years ago • 12 comments

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

flash-gordon avatar Jan 15 '20 12:01 flash-gordon

If it's ok, I'll give a try to it.

waiting-for-dev avatar Jan 16 '20 07:01 waiting-for-dev

Sure.

flash-gordon avatar Jan 16 '20 07:01 flash-gordon

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?

waiting-for-dev avatar Jan 16 '20 17:01 waiting-for-dev

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?

flash-gordon avatar Jan 16 '20 17:01 flash-gordon

rule.failure? or something along the lines could be another approach

flash-gordon avatar Jan 16 '20 17:01 flash-gordon

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 :)

waiting-for-dev avatar Jan 16 '20 17:01 waiting-for-dev

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?.

solnic avatar Jan 17 '20 08:01 solnic

works for me though rule.error?/rule.failure? still could be useful

flash-gordon avatar Jan 17 '20 08:01 flash-gordon

@flash-gordon why?

solnic avatar Jan 17 '20 09:01 solnic

@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 avatar Jan 17 '20 09:01 flash-gordon

@flash-gordon ok makes sense

solnic avatar Jan 17 '20 10:01 solnic

@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?.

flash-gordon avatar Jan 17 '20 17:01 flash-gordon