These are chat archives for dry-rb/chat

5th
Jun 2016
Tanner Donovan
@ttdonovan
Jun 05 2016 02:00
I’ve just started playing around with Crystal and wanted to get some thoughts on porting some of the dry-rb gems .i.g. https://github.com/ttdonovan/dry-monads.cr
could shards be part of dry-rb or would there be any objection to spin-off a dry-cr
Gilbert Komin
@myf9000
Jun 05 2016 06:07
Hi, everyone. I have small problem with form validation. I have optional array which can be empty or not - and when I want to use optional(:employee_attribiutes).maybe(:array?) - in test when I send empty params (nil) for this array - I’ve got a error: NoMethodError: undefined method `[]' for nil:NilClass. What I did wrong? :)
Tim Riley
@timriley
Jun 05 2016 06:28
@myf9000 that test is not actually a fathful replication of a form post, since they can never send nil - always an empty string in cases like that. If you pass an empty string, does it work?
The dry-validation form schemas are meant to handle input from rack-processed forms posts.
Gilbert Komin
@myf9000
Jun 05 2016 20:17

Thanks for answer but is any possibility to do something like that :

optional(:employee_attributes).maybe(:array?).each do
        key(:attribute_name).required(:str?)
        key(:value).required
        optional(:order).maybe(:int?)
 end

When I have in params employee_attributes I want to check its attributes, and when I have "" or nil or [] - return true. I tried to write my custom rule but it doesn’t work for array.

     optional(:employee_attributes).maybe(:array?)

      rule(present_employee_attributes: [:employee_attributes]) do |attributes|
        attributes.is_present?
      end

      configure do
        def is_present?(attributes)
          return true if attributes.empty?
          attributes
        end
      end

What I'm doing wrong?

Thanks to @flash-gordon, @mrbongiolo, @joevandyk and @ttdonovan!
Piotr Solnica
@solnic
Jun 05 2016 23:01
@myf9000 maybe + each is not supported yet, I'll be adding this for 0.8.0 release
btw use schema inside each, nested key syntax will be gone in 0.8.0