Skip to content
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

Injecting dependencies using dry-auto_inject with reserved names #707

Open
flash-gordon opened this issue Apr 15, 2022 · 0 comments
Open
Labels
Milestone

Comments

@flash-gordon
Copy link
Member

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

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 <class:Contract>': undefined method `macros' for #<Object:0x00000001114c9660> (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__.

@flash-gordon flash-gordon added this to the 2.0.0 milestone Apr 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant