These are chat archives for dry-rb/chat

6th
Apr 2018
Mateusz Kluge
@inclooder
Apr 06 2018 07:36
Hello, Is it possible to set default value for key in dry-validation ? For example optional(:enabled).maybe(:bool?) to be false by default?
Lairan
@alex-lairan
Apr 06 2018 08:42

You can use http://dry-rb.org/gems/dry-validation/input-preprocessing/

optional(:enabled, FalseBoolean).maybe(:bool?)

Mateusz Kluge
@inclooder
Apr 06 2018 08:47
How would you define FalseBolean ? Dry::Types.module::Bool.default(false) or Dry::Types.module::Strict::Bool.default(false) ?
Does it matter ?
Lairan
@alex-lairan
Apr 06 2018 09:04

I don't know, sorry :s

An example of what i've done:

module Types
  include Dry::Types.module

  Checkbox = Types::Bool.constructor do |str|
    str == '1' || str == true || str == 'true'
  end
end
I think you can do something like
FalseBolean = Types::Bool.constructor do |value|
  value || false
end
Mateusz Kluge
@inclooder
Apr 06 2018 10:08
Thank you @alex-lairan I'll check if that solves my problem. Have a good day :+1:
Lairan
@alex-lairan
Apr 06 2018 12:18
:+1: