apotonick / disposable

Decorators on top of your ORM layer.
MIT License
171 stars 39 forks source link

Avoid throwing an exception for failed coercions with dry-types 1.x #93

Open shepmaster opened 1 year ago

shepmaster commented 1 year ago

dry-types 0.15 and 1.x changed how failed coercions are reported. Older versions returned the original value while newer versions raise an exception.

0.14.1:

Types::Params::Integer['cow']
 # => "cow"

1.7.1:

Types::Params::Integer['cow']
 # *** Dry::Types::CoercionError Exception: invalid value for Integer(): "cow"

Fortunately, the constructor accepts an optional block:

When a block is passed, {#call} will never throw an exception on failed coercion, instead it will call the block.

Doubly-fortunately, the block appears to be ignored on older versions of dry-types.

To preserve the current behavior, we update to use a block that returns the original value.

apotonick commented 1 year ago

In CI I see two failures:

   1) Failure:
without Type With Nilify#test_0001_raise error for new dry-types v - work as expected for older versions [/home/runner/work/disposable/disposable/test/twin/coercion_test.rb:138]:
Dry::Types::CoercionError expected but nothing was raised.

  2) Failure:
CoercionTypingTest#test_0001_anonymous [/home/runner/work/disposable/disposable/test/twin/coercion_test.rb:184]:
Dry::Types::ConstraintError expected but nothing was raised.

By always adding that "soft block" we apparently changed expected behavior, need to check out what's the best way to do this.

thanks for this PR! :green_heart: