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

Incorrect key validation for schemas in maybe(:array) #455

Open
yauhenininjia opened this issue Mar 24, 2023 · 1 comment
Open

Incorrect key validation for schemas in maybe(:array) #455

yauhenininjia opened this issue Mar 24, 2023 · 1 comment

Comments

@yauhenininjia
Copy link

Describe the bug

Basically it is a reopening of the issue described (and reported as fixed) in #300

Valid keys of nested optional array set via maybe(:array).each(schema) are reported as incorrect. It occurs in latest version 1.13.0 as well

To Reproduce

NestedSchema = Dry::Schema.define do
  required(:bar).filled(:string)
end

BrokenSchema = Dry::Schema.define do
  config.validate_keys = true
  required(:foo).maybe(:array).each(NestedSchema)
end

BrokenSchema.call({ foo: [{ bar: "bar" }] }).errors.to_h

#=> {:foo=>{0=>{:bar=>["is not allowed"]}}}

Expected behavior

errors for the snippet above are empty

My environment

  • Affects my production application: NO
  • Ruby version: 3.1.0
  • OS: macos 11.6.7
@JacobAlexander
Copy link

JacobAlexander commented Jul 2, 2023

Use Dry::Schema.Params

NestedSchema = Dry::Schema.define do
  required(:bar).filled(:string)
end

BrokenSchema = Dry::Schema.Params do
  required(:foo).maybe(:array).each(NestedSchema)
end

BrokenSchema.call({ foo: [{ bar: 123 }] }).errors.to_h
# => {:foo=>{0=>{:bar=>["must be a string"]}}}

BrokenSchema.call({ foo: [{ bar: '123' }] }).errors.to_h
# => {}

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