# Dry::Schema that coerces to Maybe(some\_value)

**URL:** <https://discourse.dry-rb.org/t/dry-schema-that-coerces-to-maybe-some-value/1464>\
**Category:** Support\
**Created:** [May 25, 2022, 6:52pm UTC](https://discourse.dry-rb.org/t/dry-schema-that-coerces-to-maybe-some-value/1464 "2022-05-25T18:52:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris.Weitzel](https://avatars.discourse-cdn.com/v4/letter/c/5f8ce5/32.png) [@Chris.Weitzel](https://discourse.dry-rb.org/u/Chris.Weitzel)\
**Post date:** [May 25, 2022, 6:52pm UTC](https://discourse.dry-rb.org/t/dry-schema-that-coerces-to-maybe-some-value/1464/1 "2022-05-25T18:52:13Z")

</div>

Hello and thank you for any help.

Context: I am using Dry-Schema (1.8.0) to perform some input validation on structure/type, but nothing more complicated. I am also using Dry-Monads (1.4.0) for Results and Maybes.

Goal: I would LIKE to have a schema that checks a given value and requires either, a Maybe containing the correct type, an unwrapped value of the correct type, nil and then coerces that input to a Maybe containing the correct type (or None of course)

I have tried: I loaded the `:maybe` extension into my types module and I can create a Maybe version of the type class. Using this class works mostly as expected but it does seem to accept a Some containing the wrong type. Additionally, using this type in a schema does not work as I expected

Example: (This example uses Integer, but I would like to be able to use this with my own type from my application)

```auto
module Types
  Dry::Types.load_extensions(:maybe)
  Dry::Schema.load_extensions(:monads)
  include Dry.Types
end

int_schema = Dry::Schema.Params do
  required(:field).maybe(Types::Strict::Integer.maybe)
end

from_type_value = Types::Strict::Integer.maybe[1] #Some(1)
from_type_nil = Types::Strict::Integer.maybe[nil] #None
from_type_some = Types::Strict::Integer.maybe[Some(1)] #Some(1)
from_type_none = Types::Strict::Integer.maybe[None()] #None
#from_type_wrong = Types::Strict::Integer.maybe["wrong"] #raises an error as expected
from_type_wrong_some = Types::Strict::Integer.maybe[Some("wrong")] #Some("wrong") This is unexpected

from_schema_value = int_schema.call(field: 1).to_monad #Failure :field => "must be an integer
from_schema_nil = int_schema.call(field: nil).to_monad #Failure :field => "must be an integer
from_schema_some = int_schema.call(field: Some(1)).to_monad #Failure :field => "must be an integer
from_schema_none = int_schema.call(field: None()).to_monad #Failure :field => "must be an integer
from_schema_wrong = int_schema.call(field: "wrong").to_monad #Fails as expected
from_schema_wrong_some = int_schema.call(field: Some("wrong")).to_monad #Fails as expected

```

Am I misunderstanding how I should be configuring this? Any help is appreciated

---

<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:** [May 27, 2022, 1:44pm UTC](https://discourse.dry-rb.org/t/dry-schema-that-coerces-to-maybe-some-value/1464/2 "2022-05-27T13:44:47Z")

</div>

In a nutshell, they are not compatible. dry-schema is not designed with this case in mind, it doesn’t properly handle maybe types from dry-types. Perhaps, it’s possible to change something in the predicate compiler but I’m not sure. Anyway, at the moment this won’t work ☹
