Replace constructor_type with types/keys transformations #64
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This removes
constructor_type
in favor of these two methods:Struct.transform_types
—transforms all new attribute types with an arbitrary block (usesDry::Types::Schema#with_type_transform
).Struct.transform_keys
—transforms input keys with a block (usesDry::Types::Hash#with_key_transform
).See dry-rb/dry-types#231
For unknown keys it is possible to make the underlying schema strict:
You always can create a base class for strict structs:
Also, individual attributes can be omitted with adding
omittable: true
to theirs meta:You can combine it with
tranform_types
for getting a struct not raising an error on missing keys:P.S. Note that in the new version of
dry-types
default types in hash schema are only used/evaluated when a key is missing, not when the value isnil
:If you want to evaluate default value on
nil
, use.constructor
:Undefined
is treated as a missing value, that's how it works internally.Remember, you can automate "constructing" with
transform_types
: