Skip to content

Commit

Permalink
Allows Default to return the right Result
Browse files Browse the repository at this point in the history
Fixes instances when a Default type runs `#try` with a failing value.

```ruby
Types::Integer.default(1).try("a")
```
  • Loading branch information
elcuervo committed Jul 24, 2024
1 parent fdc3f26 commit 1b6766b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/dry/types/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def default?
#
# @api public
def try(input)
success(call(input))
type.try(input)
end

# @return [Boolean]
Expand Down
12 changes: 12 additions & 0 deletions spec/dry/types/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@
end
end

describe "#try" do
subject(:type) { Dry::Types["integer"].default(1) }

it "returns a success result for valid input" do
expect(type.try(5)).to be_success
end

it "returns a failure result for invalid input" do
expect(type.try("five")).to be_failure
end
end

describe "#with" do
subject(:type) { Dry::Types["nominal.time"].default { Time.now }.meta(foo: :bar) }

Expand Down

0 comments on commit 1b6766b

Please sign in to comment.