These are chat archives for dry-rb/chat

Sep 2017
Pavel Penkov
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
Sep 20 2017 14:22
class Order < Dry::Struct
    attribute :code,      Types::Coercible::String
    attribute :quantity, Types::Coercible::Int
end{"code" => "c0d3", "quantity" => "30"})
# => *** Dry::Struct::Error Exception: [] :code is missing in Hash input "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 { attributes_for(:order) }
Nuno Costa
Sep 20 2017 14:34
Another question is, is it possible to have sets of objects which are themselves Dry::Structs? Just like this
Nuno Costa
Sep 20 2017 15:05
@nunommc I think I found the answer myself using Types::Strict::Array.member(Types::Coercible::Int)
Piotr Solnica
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