0

I have the following contract and I would like to somehow set apikey to default to whatever is set in ENV.fetch('MY_ENV') so that users don't have to pass apikey param to every contract. I'd like it be injected automatically somehow if possible.

require 'dry-validation'

class BaseContract < Dry::Validation::Contract
  params do
    required(:apikey).value(:string)
  end

  rule(:apikey) do
    key.failure("must provide 'apikey'. Please set 'TICKETMASTER_API_KEY' in your environment")
  end
end

class Event < BaseContract
  params do
    required(:id).value(:string)
  end
end

event = Event.new
event.call(id: '123') # <= I'd like :apikey to be automatically set here

Is it possible?

jedi
  • 2,003
  • 5
  • 28
  • 66
  • 1
    Probably what I am trying to achieve is not the way I should approach it but anyway, I can just merge this one key `input.merge(apikey: apikey)` before calling event validations. – jedi Jan 09 '23 at 10:30

0 Answers0