These are chat archives for dry-rb/chat

26th
Sep 2016
Aleksandar Radunović
@aradunovic
Sep 26 2016 18:01

with input like this:

{ name: “my name”, products: [{ id: 1, quantity: 5 }, { id: 5, quantity: 1 }] }

how can I incorporate each validation for products into:

Form = Dry::Validation.Form do
  required(:name).filled(:str?)
  # products array validation
end

the only example I could find was array as input, but in my case, only one attr is array...

Aleksandar Radunović
@aradunovic
Sep 26 2016 18:08
also there is nested data example, but it only shows how to validate simple array of strings
Egor Lynko
@flexoid
Sep 26 2016 18:10
@aradunovic try this
required(: products).each do
  schema do
    required(:id).filled(:int?)
    required(:quantity).filled(int?)
  end
end
Aleksandar Radunović
@aradunovic
Sep 26 2016 18:11
got it…
required(:products).each do
  required(:id).filled(:int?)
  required(:quantity).filled(int?)
end
@flexoid Thanks for the effort :D
Egor Lynko
@flexoid
Sep 26 2016 18:12
if I remember correctly, better to use my version, with schema
as each without schema will not work on latest versions
please, correct me if I'm wrong
Aleksandar Radunović
@aradunovic
Sep 26 2016 18:14
it works with 0.10.2
@flexoid I’ll stick with schema do, but maybe @solnic could explain the difference
Piotr Solnica
@solnic
Sep 26 2016 18:23
Yes pls use schema whenever you define nested structures
Rafael George
@cored
Sep 26 2016 18:27
@solnic @flash-gordon mention that there's still work to be done for having optional dry struct inside dry-validation
what is missing specifically ?
Piotr Solnica
@solnic
Sep 26 2016 18:32
It is already optional
In 0.10.x
We're missing some features and test cov is so-so
Rafael George
@cored
Sep 26 2016 18:37
I see
I'm still figuring out what @flash-gordon did
Nikita Shilnikov
@flash-gordon
Sep 26 2016 19:01
I just patched a couple of classes with modules that check if we deal with struct objects, that's it. It's not a nice thing to do though, fwiw
this can be improved as we add more features
John Griffin
@john-griffin
Sep 26 2016 19:15
whats the recommended way to handle default values with dry-validation?
John Griffin
@john-griffin
Sep 26 2016 19:28
like if I omit a key during validation is there a way to get it to have that key in the output with a default/coerced value?
Taylor Lodge
@UberMouse
Sep 26 2016 21:27
Are custom predicates for dry-validation supported with .each? If I try to use one, one that worked fine with .filled, I get an undefined_method 'to_ast' for #<UnboundMethod>
Piotr Solnica
@solnic
Sep 26 2016 21:57
@UberMouse can you show some code where it happens?
Taylor Lodge
@UberMouse
Sep 26 2016 21:59
Here is the schema I'm working on, the required(:contents).each(:foo?) is what's causing the error, if I do required(:contents).filled(:foo?) it works fine https://gist.github.com/UberMouse/3e532cf21a9eebcf70012e0b3949ca17
(the valid_block? one is what I'm actually trying to use)
Also just updated the gist with the stack trace if that's helpful
Piotr Solnica
@solnic
Sep 26 2016 23:45
Thanks. I'll look into that