Skip to content

Instantly share code, notes, and snippets.

@lucianghinda
Created March 26, 2023 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucianghinda/fad23bbfd9221416a918354703e367c4 to your computer and use it in GitHub Desktop.
Save lucianghinda/fad23bbfd9221416a918354703e367c4 to your computer and use it in GitHub Desktop.
Example of using custom dry types
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