diff --git a/lib/dry/types/array/member.rb b/lib/dry/types/array/member.rb index 7f042d2e..7f7b7422 100644 --- a/lib/dry/types/array/member.rb +++ b/lib/dry/types/array/member.rb @@ -35,12 +35,12 @@ def valid?(value) def try(input, &block) if input.is_a?(::Array) result = call(input, :try) + output = result.map(&:input) if result.all?(&:success?) - output = result.map(&:input) success(output) else - failure = failure(input, result.select(&:failure?)) + failure = failure(output, result.select(&:failure?)) block ? yield(failure) : failure end else diff --git a/lib/dry/types/constrained/coercible.rb b/lib/dry/types/constrained/coercible.rb index 0e7ff006..8c0eebb6 100644 --- a/lib/dry/types/constrained/coercible.rb +++ b/lib/dry/types/constrained/coercible.rb @@ -1,14 +1,12 @@ module Dry module Types class Constrained - include Type - class Coercible < Constrained # @param [Object] input # @param [#call,nil] block # @yieldparam [Failure] failure # @yieldreturn [Result] - # @return [Result,Logic::Result,nil] + # @return [Result,nil] def try(input, &block) result = type.try(input) @@ -18,7 +16,8 @@ def try(input, &block) if validation.success? result else - block ? yield(validation) : validation + failure = failure(result.input, validation) + block ? yield(failure) : failure end else block ? yield(result) : result diff --git a/spec/dry/types/constrained_spec.rb b/spec/dry/types/constrained_spec.rb index 3989893b..296801ab 100644 --- a/spec/dry/types/constrained_spec.rb +++ b/spec/dry/types/constrained_spec.rb @@ -137,5 +137,9 @@ it 'raises when a given constraint is violated' do expect { type[%w(a b)] }.to raise_error(Dry::Types::ConstraintError) end + + it 'coerces values' do + expect(type.try(%i(foo aa)).input).to eql(%w(foo aa)) + end end end