interactor-contracts
interactor-contracts copied to clipboard
Default values
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.
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.
Can I work on this?