interactor-contracts icon indicating copy to clipboard operation
interactor-contracts copied to clipboard

Default values

Open Fodoj opened this issue 5 years ago • 2 comments

Is it possible to set default values? I would like to set certain constraints on incoming parameters while not requiring to fill in any of them, falling back to defaults instead.

Fodoj avatar Sep 04 '20 21:09 Fodoj

Default values aren't very intuitive in the dry-validation ecosystem. I think the easiest way to handle defaults (for required values) is probably the following. Assuming you have a status that you want to take as an argument with the default of 'processing':

module Types
  include Dry.Types()

  Status = String.default('processing'.freeze)
end

class MyCommand < Interactor
  include Interactor::Contracts

  expects do
    required(:status).value(Types::Status)
  end
end

You can also handle a default value using optional(:status).maybe(:str?) and a rule to set it if it's missing, but I think that would take some internal refactoring in order to expose the rules interface within interactor-contracts. The interface was designed prior to the extraction of dry-schema from dry-validation so it doesn't take them into account.

The type system of dry-types is really powerful. I suggest checking it out!

Let me know if that doesn't work for you.


I would also be happy to help coach you through the reworking on the internals of the gem to expose rules if you're interested. I labeled this issue with hacktoberfest because it's something I'm interested in and likely something I will suggest to my team if they are interested in Hacktoberfest.

michaelherold avatar Sep 05 '20 01:09 michaelherold

Can I work on this?

Khush1801 avatar Sep 20 '20 18:09 Khush1801