dry-rb / dry-rb/dry-validation

Macros do not support validation on root object

Open
#688 1 comment 1 reaction 0 assignees View on GitHub
bug help wanted
Dominant language
Ruby
Stars
1.4k
Forks
195
PR merge metrics
No merged PRs in 30d

Description

## Describe the bug

I extracted a macro into a reusable module, like this:
~~~ruby
module ReusableRule
extend ::ActiveSupport::Concern

included do
register_macro(:reusable_macro) do
# do some validations here
end
end
end
~~~

and use it like this:
~~~ruby
class SomeContracr < ::Dry::Validation::Contract
params do
# define some params here
end

include ::ReusableRule
rule(path_to: :inner_hash).validate(:reusable_macro)
end
~~~
which works fine.

But I cannot use it on the root hash, like this:
~~~ruby
class SomeContracr < ::Dry::Validation::Contract
params do
# define some params here
end

include ::ReusableRule
rule.validate(:reusable_macro)
end
~~~

## Expected behavior

The macro should validate the root hash the same way as when I use it on an inner hash.

## Actual behavior

dry-validation throws an `ArgumentError`
~~~
lib/dry/validation/values.rb:59:in `[]': +key+ must be a valid path specification (ArgumentError)
~~~

## My environment

- Ruby version: 3.0
- OS: Docker image (Debian)

## Suggestion
Probably adding
~~~ruby
return data if args.size < 1
~~~
around [here](https://github.com/dry-rb/dry-validation/blob/c3b5a6/lib/dry/validation/values.rb#L47).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.