-
-
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
Support for "validate" helper within rule blocks #679
Comments
Will it be possible to call |
smth like |
@musaffa yes! I just updated the example but I think @flash-gordon has a point and we should have |
I monkey-patched module Dry
module Validation
class Evaluator
def validate(*args)
macros = args.each_with_object([]) do |spec, macros|
case spec
when Hash
spec.each do |k, v|
macros << [k, v.is_a?(Array) ? v : [v]]
end
else
macros << Array(spec)
end
end
macros.each do |args|
macro = macro(*args.flatten(1))
instance_exec(**macro.extract_block_options(_options.merge(macro: macro)), ¯o.block)
end
end
end
end
end Then you can do: rule(:age) do
validate(:filled?, gt?: 18)
end I just took these methods from I want to refactor it, write tests and docs and open a PR, but I wanted to clear this out with y'all first: I think in |
I forked and created a branch if you want to take a look: main...zavan:dry-validation:macros-within-rule No tests yet and it breaks the existing ones relying on |
It would be great to have this. If you could open a PR with fixed tests that would be awesome. |
Support for executing predicates using
validate
method withinrule
blocks will make it much easier to apply various checks with extra conditions:See the original conversation: #678
The text was updated successfully, but these errors were encountered: