Created
March 26, 2023 07:10
-
-
Save lucianghinda/fad23bbfd9221416a918354703e367c4 to your computer and use it in GitHub Desktop.
Example of using custom dry types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'dry/schema' | |
require 'dry/types' | |
class MyObject | |
def initialize(value) | |
@value = value | |
end | |
def value | |
return @value if @value.is_a?(Array) | |
@value.split("|") | |
end | |
end | |
module Types | |
include Dry.Types() | |
end | |
MyObjectType = Types.Constructor(MyObject) | |
TypeContainer = Dry::Schema::TypeContainer.new | |
TypeContainer.register('params.my_object_type', MyObjectType) | |
MySchema = Dry::Schema.Params do | |
config.types = TypeContainer | |
required(:received).filled(:my_object_type) | |
end | |
result = MySchema.call({ | |
received: "string|to|split" | |
}) | |
puts result.inspect | |
puts result[:received].value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment