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

Inconsistent errors using struct extension #424

Open
007lva opened this issue Sep 2, 2022 · 4 comments
Open

Inconsistent errors using struct extension #424

007lva opened this issue Sep 2, 2022 · 4 comments

Comments

@007lva
Copy link

007lva commented Sep 2, 2022

Describe the bug

Inconsistent errors using struct extension with nested arrays and query parameters. No issues with single element arrays.

To Reproduce

class FilterStruct < Dry::Struct
  attribute? :candidate_ids, Types::Array.of(Types::Params::Integer)
end

class FiltersSchema < Dry::Schema::Params
  Dry::Schema.load_extensions(:struct)

  define do
    optional(:filters).hash(FilterStruct)
  end
end

FiltersSchema.new.call(filters: { candidate_ids: ["1"]}).errors.to_h # "1" is valid (single element array)
=> {}
FiltersSchema.new.call(filters: { candidate_ids: ["m", "1"]}).errors.to_h # now "1" is invalid:
=> {:filters=>{:candidate_ids=>{0=>["must be an integer"], 1=>["must be an integer"]}}}

Expected behavior

Using struct extension, I would expect:

FiltersSchema.new.call(filters: { candidate_ids: ["1"]}).errors.to_h # "1" is valid:
=> {}
FiltersSchema.new.call(filters: { candidate_ids: ["m", "1"]}).errors.to_h # "1" is still valid:
=> {:filters=>{:candidate_ids=>{0=>["must be an integer"]}}}

which would be consistent with errors when struct extension is not being used:

class FiltersHashSchema < Dry::Schema::Params
  define do
    optional(:filters).hash do
      optional(:candidate_ids).filled(Types::Array.of(Types::Params::Integer))
    end
  end
end

FiltersHashSchema.new.call(filters: { candidate_ids: ["1"]}).errors.to_h # "1" is valid:
=> {}
FiltersHashSchema.new.call(filters: { candidate_ids: ["m", "1"]}).errors.to_h # "1" is still valid:
=> {:filters=>{:candidate_ids=>{0=>["must be an integer"]}}}

My environment

  • Affects my production application: NO
  • Ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux-musl]
@007lva
Copy link
Author

007lva commented Nov 18, 2022

I've just confirmed that still an issue in v1.12.0.

@solnic
Copy link
Member

solnic commented Nov 26, 2022

Please check with 1.13.0

@007lva
Copy link
Author

007lva commented Nov 26, 2022

I've just confirmed it with 1.13.0 as well.

@007lva
Copy link
Author

007lva commented May 28, 2024

Also confirmed with 1.13.4.

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

2 participants