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

Error output discards useful information from Enum types #469

Open
paul opened this issue Sep 7, 2023 · 0 comments
Open

Error output discards useful information from Enum types #469

paul opened this issue Sep 7, 2023 · 0 comments

Comments

@paul
Copy link
Contributor

paul commented Sep 7, 2023

Describe the bug

When using an Enum type with a schema, the output about the allowed values gets lost, and the error is reduced to "must be a string"

To Reproduce

module Types
  include Dry.Types()

  Status = Types::String.enum(
    "Queued" => 0,
    "Scheduled" => 1
  )
end

MySchema = Dry::Schema.Params do
  required(:status).filled(Types::Status)
end

class MyStruct < Dry::Struct
  attribute :status, Types::Status
end
Types::Status[0]
# => "Queued"
Types::Status[42]
# => Dry::Types::ConstraintError
# 42 violates constraints (type?(String, 42) AND included_in?(["Queued", "Scheduled"], 42) failed) 

bad_data = {status: 42}
MyStruct.new(bad_data)
# => Dry::Struct::Error [MyStruct.new] 
# 42 (Integer) has invalid type for :status violates constraints (type?(String, 42) AND included_in?(["Queued", "Scheduled"], 42) failed)>

MySchema.call(bad_data)
# => #<Dry::Schema::Result{:status=>42} errors={:status=>["must be a string"]} path=[]>

"status must be a string" isn't really true, and is misleading to what the problem really the (the value isn't one of the acceptable values for the enum)

Expected behavior

It would be nice if the error output for the enum had a better description of what was wrong. Something like "must be one of String Enum["Queued" => 0|"Scheduled" => 1]"

My environment

  • Affects my production application: YES, not urgently
  • Ruby version: 3.2.2
  • OS: Linux
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