Skip to content

Instantly share code, notes, and snippets.

@stevelounsbury
Created October 10, 2017 15:28
Show Gist options
  • Select an option

  • Save stevelounsbury/a4b4e6e2824a1c8e136abafbf100d297 to your computer and use it in GitHub Desktop.

Select an option

Save stevelounsbury/a4b4e6e2824a1c8e136abafbf100d297 to your computer and use it in GitHub Desktop.
require 'dry-types'
module Types
include Dry::Types.module
end
hash = Types::Hash.schema(name: Types::Strict::String, age: Types::Coercible::Int)
#
# When providing an invalid value for Strict types, a SchemaError is raised letting us know which key had the issue.
#
hash[name: {}, age: 13]
# => Dry::Types::SchemaError: {} (Hash) has invalid type for :name violates constraints (type?(String, {}) failed)
#
# When providing an uncoercible value for Coercible types, TypeError bubbles up but we don't know which key caused
# the problem.
#
hash[name: "Test", age: {}]
# => TypeError: can't convert Hash into Integer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment