These are chat archives for dry-rb/chat

21st
Aug 2017
siassaj
@siassaj
Aug 21 2017 02:12
@dikond that's about right I guess, but how did you find that syntax?
i've searched for a while, never found anything online to show me how to define a new type
eg I'd like to define a 'time_only' type separate to ruby's Time that has date too. So i've no idea how to do that with Dry Types
Opan Mustopah
@opan
Aug 21 2017 04:01
Hi guys, I have a little question about dry-validation. Is there possible to make every key that have nil value removed from the output? Something like this:
schema = Dry::Validation.Form do
  required(:email).filled(:str?)

  optional(:username).maybe(:str?)
end

result = schema.call('email' => 'sample@email.com', 'username' => nil)

puts result.output
# The result must only show output email, without 'username' because it's have nil value
# {email: 'sample@email.com'}
Tim Riley
@timriley
Aug 21 2017 04:35
@opan dry-validation doesn’t do that, but you could run your validation output through some kind of mapping method to do that
siassaj
@siassaj
Aug 21 2017 05:37
@opan yeah @timriley is right, the validation shouldn't be responsible for that. In my mind result is either "success" or "error" with reasons. The input hash can be mapped over once it's been deemed to be valid.
Opan Mustopah
@opan
Aug 21 2017 05:41
@timriley @siassaj Ah okay. Thanks for the input! Will try that
siassaj
@siassaj
Aug 21 2017 05:44
simplest case; params..select{|k,v| v.present?}
Opan Mustopah
@opan
Aug 21 2017 06:09

simplest case; params..select{|k,v| v.present?}

Great!

Danny
@dikond
Aug 21 2017 07:57
@siassaj it's not ducumented, but you can always check the source if something isn't clear. Specs are worth to look at, usually, e.g. https://github.com/dry-rb/dry-types/blob/52fec7086a0edba29fdc58e8090d5006252640d4/spec/dry/types_spec.rb
siassaj
@siassaj
Aug 21 2017 07:58
thank you danny
@dikond
Danny
@dikond
Aug 21 2017 07:58
You are welcome!
siassaj
@siassaj
Aug 21 2017 07:59
this ought to be on docs, it makes dry-types much more useful
Danny
@dikond
Aug 21 2017 07:59
Agree