Hi there,
been working on the dry-v/types upgrade for Trailblazer gems (Reform and Disposable) and found an interesting “problem” (maybe not a problem) about nilify.
Disposable has a coercion feature where you can pass either type
and/or nilify
and it uses dry-types for the coercion but for new dry-type’s versions, this has changed a lot.
Disposable basically works quite similar to dry-struct so here a super simple example:
class MyTwin < Disposable::Twin
property :title, nilify: true
end
With dry-types < 1.0 this was returning this:
MyTwin.new(title: 'title').title -> 'title'
MyTwin.new(title: '').title -> nil
MyTwin.new(title: nil).title -> nil
This because we are using a setter that it will do something like this:
Types::Form::Nil.call(value) if nilify
Now for dry-types this “should” be Types::Params::Nil.call(value)
but this will raise an error if value is not nil which breaks a lot of people projects.
Tried to use Nominal
but it doesn’t actually nilify since ''
are not coerced into nil
.
How can I reproduce the same behaviour using newer versions of dry-types (do not raise an error if passing a not nil value)?
Thank you
Cheers,