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

Default values #35

Open
Fodoj opened this issue Sep 4, 2020 · 2 comments
Open

Default values #35

Fodoj opened this issue Sep 4, 2020 · 2 comments
Labels
documentation hacktoberfest Let's hack together for Hacktoberfest!

Comments

@Fodoj
Copy link

Fodoj commented Sep 4, 2020

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.

@michaelherold michaelherold added documentation hacktoberfest Let's hack together for Hacktoberfest! labels Sep 5, 2020
@michaelherold
Copy link
Owner

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.

@Khush1801
Copy link

Can I work on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation hacktoberfest Let's hack together for Hacktoberfest!
Projects
None yet
Development

No branches or pull requests

3 participants