These are chat archives for dry-rb/chat

11th
Apr 2016
My goal is to try to get form input values into a dry-type struct
and validate the data
any ideas?
Joe Van Dyk
@joevandyk
Apr 11 2016 01:03
One of the things I’m building is a tier system, which is basically a collection of those tiers. One of the validations I have is that tiers with higher quantity have to have a higher percentage. i.e. if I have 3@5% and 8@10%, 10@11% would be fine, but 9@11% would not be. What’s the best way to do that sort of validation?
Piotr Solnica
@solnic
Apr 11 2016 11:23
@joevandyk please report an issue, this shouldn’t crash
Tim Riley
@timriley
Apr 11 2016 12:45
So here’s a thing
schema = Dry::Validation.Form do
  key(:contributors).each do
    schema do
      key(:contributor_id).required(:int?)
    end
  end

  configure do
    def call(input)
      input = input.dup
      input["contributors"] = [] unless input.key?("contributors")

      super(input)
    end
  end
end

input = {}
result = schema.(input)
# => #<Dry::Validation::Result output={:contributors=>[]} messages={}>
In HTML forms it seems there’s no way to properly submit an empty array.
So instead of some blank value being submitted with the key, it just doesn’t get submitted at all.
And unlike checkboxes with the hidden input you can place above them to ensure a default “off” value is submitted, with arrays there seem to be no such workarounds.
At least as far as I can tell.
So this is the best I could come up with for a schema where you really do want the array’s key to be present.
Question: is that either terrible or OK? :grimacing:
Tim Riley
@timriley
Apr 11 2016 13:00
Actually, I wonder if this sort of thing is what the input_processor is meant for...
Except I think that needs to be set to :form for the form input processor compiler to run.
Piotr Solnica
@solnic
Apr 11 2016 13:21
@timriley please report an issue
Tim Riley
@timriley
Apr 11 2016 13:21
For the general use case?
Piotr Solnica
@solnic
Apr 11 2016 13:43
Yes
Tim Riley
@timriley
Apr 11 2016 13:54
Done!
Piotr Solnica
@solnic
Apr 11 2016 13:54
@timriley I guess we should coerce nil to empty array
Fran Worley
@fran-worley
Apr 11 2016 14:11
@solnic did you ever get anywhere with passing options to a message? I have a validation that tries to pass a Liquid Template and fails if Liquid has any errors. I'd love to be able to pass the Liquid message back to the user rather than my very unhelpful Your liquid template is invalid message set via my Yml file. Any thoughts?
Piotr Solnica
@solnic
Apr 11 2016 14:11
@fran-worley planned for 1.0
niedhui
@niedhui
Apr 11 2016 14:54
Hi, I was trying to switch Grape's coercible feature from virtus to dry-types.
Grape support mutiple candidate coercbile type,like requires :a, types: [Integer, String],
first it will try coerces input to Integer, if fails , try coerces to String.
I was planning to implements this using the Sum type.
(Dry::Types['coercible.int'] | Dry::Types['coercible.string'].call 'something' raise ArgumentError
I also tried Dry::Types['coercible.int'].safe or Dry::Types['maybe.coercible.int'], also failed with ArgumentError
Piotr Solnica
@solnic
Apr 11 2016 14:58
irb(main):007:0> (Types::Form::Int | Types::String)[1]
=> 1
irb(main):008:0> (Types::Form::Int | Types::String)["foo"]
=> “foo"
irb(main):009:0> (Types::Form::Int | Types::String)["1"]
=> 1
@niedhui ^^
you want Form or JSON, not Coercible
Coercible uses kernel coercion methods, that’s for low-level coercions, not stuff like Grape and HTTP
niedhui
@niedhui
Apr 11 2016 15:01
thanks , I'll try this,
Piotr Solnica
@solnic
Apr 11 2016 15:01
Form and JSON are also “safe”, as in, they passthrough values that they couldn’t coerce
Piotr Solnica
@solnic
Apr 11 2016 15:07
it’d be absolutely awesome if Grape switched to dry-types for coercion and dry-validation for, well, validation :D
niedhui
@niedhui
Apr 11 2016 15:11
I was doing some work to see how much it will change the Grape's internal API, very early stage :smile:
BTW, Constructor#try only rescue the TypeError, why not rescue all the StandardError? so the try will work 'more as expected'
Piotr Solnica
@solnic
Apr 11 2016 15:16
@niedhui that would be too much, our intention is to rescue from type errors, not any error
niedhui
@niedhui
Apr 11 2016 15:40
@solnic seem's Dry::Types['form.int'] is a little unexpected , (Dry::Types['form.int'] | Dry::Types['string']).call('12a') will return 12, not the string '12a'
Piotr Solnica
@solnic
Apr 11 2016 15:45
@niedhui UGH, it’s because we do ”12a”.to_i
niedhui
@niedhui
Apr 11 2016 15:50
I look at the source code, maybe I should add Dry::Types.register('grape.int', Dry::Types['int'].constructor(->(input) { Integer(input) rescue nil})) as a work around, and use it in the Grape for the first step, to make all the orignal spec pass, than discuss what this should behave like
Piotr Solnica
@solnic
Apr 11 2016 15:51
sounds good
John Backus
@backus
Apr 11 2016 22:17
Finally getting around to commenting on #99
Simon Schmid
@sled
Apr 11 2016 23:29
is it possible to "wrap" a validation schema inside Dry::Validation.Form only for the coercions? Otherwise I'd have to do everything twice, i.e for Form and JSON