dry-rb / dry-rb/dry-validation

Injecting dependencies using dry-auto_inject with reserved names

Open
#707 0 comments 1 reaction 0 assignees View on GitHub
feature
Dominant language
Ruby
Stars
1.4k
Forks
195
PR merge metrics
No merged PRs in 30d

Description

Here I'm trying to inject a dependency named `config` using the default kwargs strategy of dry-auto_inject.

```ruby
class MyContract < Dry::Validation::Contract
include Import['config']

json do
required(:foo).maybe(:string)
end

rule(:foo) do
key.failure("foo is required") if config.foo_required? && value.nil?
end
end

config = Object.new.tap do |c|
def c.foo_required?
true
end
end

contract = MyContract.new(config: config)

contract.({ foo: nil })
```

This results in

```
/Users/gordon/dev/dry-rb/dry-validation/lib/dry/validation/contract.rb:69:in `block in ': undefined method `macros' for # (NoMethodError)
```

The problem is dry-validation uses `config` as one of its constructor keywords. dry-auto_inject "guesses" it should pass the dependency down (or up?) to the parent class. This way dry-validation gets the wrong config object. I guess we should rename `config` to `__config__`, it should be an easy thing to do. This won't break backward compatibility as far as I can see. Assuming we'll add an alias `alias_method :config, :__config__`.

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.