These are chat archives for dry-rb/chat

20th
Sep 2017
Pavel Penkov
@PavelPenkov
Sep 20 2017 12:32
Hi everyone! How to perform validation of an array with different types of objects? Something like [{type: 'book', author: 'Alice'}, {type: 'album', performer: 'Bob'}]. The validation itself works i.e. it doens't pass on incorrect data, but the error messages are a mess
Nuno Costa
@nunommc
Sep 20 2017 14:22
class Order < Dry::Struct
    attribute :code,      Types::Coercible::String
    attribute :quantity, Types::Coercible::Int
end

Order.new({"code" => "c0d3", "quantity" => "30"})
# => *** Dry::Struct::Error Exception: [Order.new] :code is missing in Hash input

Order.new(code: "c0d3", quantity: "30")
# => #<Order code="c0d3" quantity=30>
I have already figured that I can pass in different "Constructor Types" and that by default is permissive
So, is there any reason why it doesn't accept normal hashes? For example, I could not get it to create my class with FactoryGirl until I changed it to subject { described_class.new attributes_for(:order) }
Nuno Costa
@nunommc
Sep 20 2017 14:34
Another question is, is it possible to have sets of objects which are themselves Dry::Structs? Just like this https://github.com/solnic/virtus#collection-member-coercions
Nuno Costa
@nunommc
Sep 20 2017 15:05
@nunommc I think I found the answer myself using Types::Strict::Array.member(Types::Coercible::Int)
Piotr Solnica
@solnic
Sep 20 2017 17:47
@PavelPenkov dry-struct is not a validation library, although it's true that we need to improve exception messages, which is planned for 1.0.0