dry-types
dry-types copied to clipboard
default causes try/1 to raise an ConstraintError exception rather than an Failure
Describe the bug
default causes try/1 to raise an ConstraintError exception rather than an Failure. This seems completely different from every other result of try
and the documentation doesn't detail this fact.
To Reproduce
Types::String.default{"x"}.try(1)
# Dry::Types::ConstraintError: 1 violates constraints (type?(String, 1) failed)
from /Users/krainboltgreene/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/dry-types-1.7.1/lib/dry/types/constrained.rb
Types::String.default("x".freeze).try(1)
# => Dry::Types::ConstraintError: 1 violates constraints (type?(String, 1) failed)
from /Users/krainboltgreene/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/dry-types-1.7.1/lib/dry/types/constrained.rb
Expected behavior
Types::String.default("x".freeze).try("")
# => #<Dry::Types::Result::Success input="">
Types::String.constrained(format: /a/i).try(1)
# => #<Dry::Types::Result::Failure input=1 error=#<Dry::Types::ConstraintError: 1 violates constraints (type?(String, 1) AND format?(/a/i, 1) failed)>>
Types::String.try(1)
# => #<Dry::Types::Result::Failure input=1 error=#<Dry::Types::ConstraintError: 1 violates constraints (type?(String, 1) failed)>>
My environment
- Affects my production application: YES
- Ruby version: 3.2