Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dry::Types::ConstraintError not propagated when originating from inside a Dry::Types::Map #190

Open
hovsater opened this issue Feb 28, 2024 · 0 comments

Comments

@hovsater
Copy link

hovsater commented Feb 28, 2024

Describe the bug

Given the following code:

module Types
  include Dry.Types

  Fruit = String.enum("apple", "banana", "pear")
end

class FruitScore < Dry::Struct
  attribute :fruit, Types::Fruit
  attribute :score, Types::Integer.constrained(gteq: 1, lteq: 10)
end

class FruitScoreboard < Dry::Struct
  attribute :scores, Types::Map(Types::String, Types::Array.of(FruitScore))
end

one would expect the following input:

FruitScoreboard.new(scores: { "john" => [{ fruit: "lemon", score: 1 }] })

to generate an error message related to the constraint being violated. Instead, we get the following error message:

/gems/dry-types-1.7.0/lib/dry/types/nominal.rb:122:in `failure': error must be a CoercionError (ArgumentError)

        raise ArgumentError, "error must be a CoercionError" unless error.is_a?(CoercionError)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The error originate from here:

https://github.com/dry-rb/dry-types/blob/ffde4175a833c6911cc889e7b1b4717bc712d3be/lib/dry/types/nominal.rb#L121-L125

and I assume error received is a MultipleError judging by this code:

https://github.com/dry-rb/dry-types/blob/ffde4175a833c6911cc889e7b1b4717bc712d3be/lib/dry/types/map.rb#L132-L136

Should the check:

raise ArgumentError, "error must be a CoercionError" unless error.is_a?(CoercionError)

perhaps be changed into this?

raise ArgumentError, "error must be a CoercionError" unless error <= CoercionError

Expected behavior

I expect a Dry::Struct::Error to be raised that propagates the actual Dry::Types::ConstraintError error.

My environment

  • Affects my production application: YES
  • Ruby version: ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-linux]
  • OS: macOS 14.3 (23D56)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant