Created
October 10, 2017 15:28
-
-
Save stevelounsbury/a4b4e6e2824a1c8e136abafbf100d297 to your computer and use it in GitHub Desktop.
This file contains hidden or 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-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