From 1b6766b2122e95f7d5d327cb540d131d775cc310 Mon Sep 17 00:00:00 2001 From: elcuervo Date: Wed, 24 Jul 2024 16:30:46 -0300 Subject: [PATCH] Allows Default to return the right Result Fixes instances when a Default type runs `#try` with a failing value. ```ruby Types::Integer.default(1).try("a") ``` --- lib/dry/types/default.rb | 2 +- spec/dry/types/default_spec.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/dry/types/default.rb b/lib/dry/types/default.rb index ff423090..7a66aa7b 100644 --- a/lib/dry/types/default.rb +++ b/lib/dry/types/default.rb @@ -79,7 +79,7 @@ def default? # # @api public def try(input) - success(call(input)) + type.try(input) end # @return [Boolean] diff --git a/spec/dry/types/default_spec.rb b/spec/dry/types/default_spec.rb index 26763b5f..3d742eba 100644 --- a/spec/dry/types/default_spec.rb +++ b/spec/dry/types/default_spec.rb @@ -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) }