# Something like .lax, but return nil instead of input

**URL:** <https://discourse.dry-rb.org/t/something-like-lax-but-return-nil-instead-of-input/1168>\
**Category:** dry-types\
**Created:** [December 3, 2020, 10:21pm UTC](https://discourse.dry-rb.org/t/something-like-lax-but-return-nil-instead-of-input/1168 "2020-12-03T22:21:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ianks](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/ianks/32/449_2.png) [@ianks](https://discourse.dry-rb.org/u/ianks)\
**Post date:** [December 3, 2020, 10:21pm UTC](https://discourse.dry-rb.org/t/something-like-lax-but-return-nil-instead-of-input/1168/1 "2020-12-03T22:21:45Z")

</div>

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:

```ruby
EmailAddress ||= begin
  Types::String
    .constructor(&:downcase)
    .constructor(&:strip)
    .constrained(format: URI::MailTo::EMAIL_REGEXP)
end

```

Currently, you can use `.lax` to rescue and return the input: `EmailAddress.lax.call('bad') #=> bad`

This is useful, but it would be much more useful to be able to nil out the value. This would make it much easier to filter our junky data that’s is not business critical.

Something like:

```ruby
EmailAddress.lax(nil: true).call('bad') #=> nil`

```

Would y’all be open to adding this?

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)\
**Post date:** [December 9, 2020, 8:34am UTC](https://discourse.dry-rb.org/t/something-like-lax-but-return-nil-instead-of-input/1168/2 "2020-12-09T08:34:27Z")

</div>

This could be considered but somebody would have to take a really close look at what kind of an impact adding `nil:` option would have on the rest of the built-in types, as well as usage in other libraries, especially dry-schema. I’m not saying it would cause problems, it’s just that I can’t promise it won’t that’s why it would have to be investigated first.

I suspect a safer option would be a dedicated type built on top of `lax` type, that would simply return `nil` if the underlying lax type returns the original input.

---

<div class="post-metadata">

**Author:** ![ianks](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/ianks/32/449_2.png) [@ianks](https://discourse.dry-rb.org/u/ianks)\
**Post date:** [December 10, 2020, 1:08am UTC](https://discourse.dry-rb.org/t/something-like-lax-but-return-nil-instead-of-input/1168/3 "2020-12-10T01:08:39Z")

</div>

Agree @solnic. What should we call it? `nillable`? `or_nil`? Something else?

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)\
**Post date:** [December 14, 2020, 2:41pm UTC](https://discourse.dry-rb.org/t/something-like-lax-but-return-nil-instead-of-input/1168/4 "2020-12-14T14:41:44Z")

</div>

@ianks I like `.or_nil` because `Types::String.constrained(format: /\d+/).or_nil` looks really sweet 🙂

---

<div class="post-metadata">

**Author:** ![ianks](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/ianks/32/449_2.png) [@ianks](https://discourse.dry-rb.org/u/ianks)\
**Post date:** [December 14, 2020, 8:46pm UTC](https://discourse.dry-rb.org/t/something-like-lax-but-return-nil-instead-of-input/1168/5 "2020-12-14T20:46:45Z")

</div>

[https://github.com/dry-rb/dry-types/pull/409](https://github.com/dry-rb/dry-types/pull/409) 😃

---

<div class="post-metadata">

**Author:** ![flash-gordon](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/flash-gordon/32/494_2.png) [@flash-gordon](https://discourse.dry-rb.org/u/flash-gordon)\
**Post date:** [December 14, 2020, 9:25pm UTC](https://discourse.dry-rb.org/t/something-like-lax-but-return-nil-instead-of-input/1168/6 "2020-12-14T21:25:19Z")

</div>

Oh, sorry, I missed the email from discourse. Pls hold on for a moment because I think I have a nice plan for this feature.

- I want to avoid extending the AST above the minimum.
- It’s a special case of [https://github.com/dry-rb/dry-types/issues/407](https://github.com/dry-rb/dry-types/issues/407)
- Which is in order a special case of the so-called “wrapper” type.

```ruby
Types::EmailAddress.wrap { |input, type| type.input { nil } }

```

- It’s not as nearly nice as `.or_nil` but we can have a syntax extension for commonly used constructors. It’s a nice thing to have.
- I already have a POC ([https://github.com/dry-rb/dry-types/compare/wrapper-type](https://github.com/dry-rb/dry-types/compare/wrapper-type)) but I’m working on a different implementation atm. I’ll try to wrap it up tonight or tomorrow morning (pun not intended). Once I get it done I’ll play with syntax extensions and we’ll see if we kill all the creatures with this boulder. 🙂

---

<div class="post-metadata">

**Author:** ![flash-gordon](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/flash-gordon/32/494_2.png) [@flash-gordon](https://discourse.dry-rb.org/u/flash-gordon)\
**Post date:** [December 15, 2020, 10:02am UTC](https://discourse.dry-rb.org/t/something-like-lax-but-return-nil-instead-of-input/1168/7 "2020-12-15T10:02:17Z")

</div>

My progress on this [https://github.com/dry-rb/dry-types/pull/410](https://github.com/dry-rb/dry-types/pull/410)
