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)
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