These are chat archives for dry-rb/chat

27th
Jun 2017
Rafael George
@cored
Jun 27 2017 14:22
hi
I'm trying to use dry::struct and was wondering if there's a possibility of using an optional array
with a custom define type member
line_items is missing in Hash input
getting that error
line_items sometimes will come with my payload but not always
so I want it to make it an optional type
I used Types::Json::Array.optional but I still get the same error
Nikita Shilnikov
@flash-gordon
Jun 27 2017 14:36
@cored for handling missing keys you need to use other constructor type, e.g. constructor_type :schema, check out the docs for more info http://dry-rb.org/gems/dry-struct/
Rafael George
@cored
Jun 27 2017 14:37
checking
thanks @flash-gordon
Rafael George
@cored
Jun 27 2017 16:02
so I have another question
I'm trying to compose an array of things, at first I thought taht usign Types::Json::Array would be enough
however I prefer to have an actual type definition so I can make calls like .line_items.first.quantity
so I defined it something like attribute :line_items, Types::Array.member(LineItem) and LineItem would be another Dry::Struct
however I'm getting this error -> undefined methodcall' for #<Class:0x007f9700ca6180>`
Nikita Shilnikov
@flash-gordon
Jun 27 2017 16:11
sorry, can't tell what's wrong without actual code
Rafael George
@cored
Jun 27 2017 16:11
ok
wait
I know there are some types that are off
like using floats for a price
I will change that as soon as I have more information
Nikita Shilnikov
@flash-gordon
Jun 27 2017 16:19
having LineItem definition after LineItems doesn't look right
Rafael George
@cored
Jun 27 2017 16:21
oh
is working
thanks
it was the order for sure
does this make sense @flash-gordon ? instead of using Types::Json::Array which was what I was using
Nikita Shilnikov
@flash-gordon
Jun 27 2017 16:24
yeah, this is fine
Rafael George
@cored
Jun 27 2017 16:24
cool
thanks
I just realize that using hashes as the top level data structure for my domain is leaking
that's the reason I decided to start using dry structs to define value objects so I can pass that around and be certain that I have proper values
Rafael George
@cored
Jun 27 2017 16:32
I jumped to another error now, looks like the constructor_type that I'm using omits the key for the output
so payload.line_items is nil instead of been []
actually
that was wasn't the issue, the issue is related on hwo I'm constructing my new type
I thought taht Payload.new(hash) would suffice
however I'm getting a payload without the ata that I'm passing to it
Rafael George
@cored
Jun 27 2017 16:38
Dry::Types::ConstraintError: nil violates constraints (default value nil violates constraints failed)
taht's the error that I'm getting
which is odd, since I specified a default value for the missing data
nvm
found the problem