[Dry-Struct] omittable attributes


#1

I started using dry-struct master branch to access some of the new behavior for a project, however, there’s some things that are a regression, I feel.

One of my use-cases is to create a Struct to parse and cast URL query params. These are mostly optional, and in the latest released version it is fairly easy to handle with constructor_type :schema. There’s no simple equivalent in master, I either have to manually set all my attributes with .meta(omittable: true) or add a .transform_types hook.

Dry::Types already allows one to specify a type as .optional, wouldn’t it perhaps be simpler to have Dry::Struct just use that to indicate if the attribute is required or not?


#2

@paul .optional is responsible for accepting nil as a value rather than key presence. It works in 100% the same way for dry-types’ hash schemas because dry-struct delegates to schemas in all respects. Note that you can define a base class/mixin for your structs that calls transform_types just once.