Closed ianks closed 3 years ago
@ianks I'm so sorry but I didn't see @flash-gordon's reply right here: https://discourse.dry-rb.org/t/something-like-lax-but-return-nil-instead-of-input/1168/5 before I suggested OrNil
could be a good addition. Could you please sync up with Nikita about it? From what I now know, he started working on something that will simply support what you need too, and will be more generic.
Would it be possible to merge this PR @flash-gordon / @solnic? Then the refactoring can make this feature work with it? This functionality is critical for us and wouldn't want to have to delay it a lot if that can be avoided.
@ianks can you pin your dependency to this branch maybe?
Now in master it can be done with
Dry::Types.define_builder(:or_nil) { _1.optional.fallback(nil) }
🎉
So there have been a few times where it would be nice to simply nullify a value if a type is invalid. Imagine we have this type:
Currently, you can use
.lax
to rescue and return the input:EmailAddress.lax.call('bad') #=> 'bad'
This is useful, but it would useful to be able to nil out the value, as well. This would make it much easier to filter our junky data that’s is not business critical.
This PR adds
#or_nil
to support this exact use casevia: https://discourse.dry-rb.org/t/something-like-lax-but-return-nil-instead-of-input/1168/2