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

Key validation incorrect for schemas in maybe(:array) #300

Closed
adamransom opened this issue Jun 26, 2020 · 2 comments
Closed

Key validation incorrect for schemas in maybe(:array) #300

adamransom opened this issue Jun 26, 2020 · 2 comments
Labels
Milestone

Comments

@adamransom
Copy link
Contributor

adamransom commented Jun 26, 2020

When specifying a schema with a "maybe" array that has a predefined schema for its each block, valid keys are reported as invalid (i.e. not allowed) keys.

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

WorkingSchema = Dry::Schema.define do
  config.validate_keys = true
  required(:foo).maybe(:array).each(:hash) do
    required(:bar).filled(:string)
  end
  required(:oof).filled(:array).each(NestedSchema)
end


puts BrokenSchema.call({ foo: [{ bar: "bar" }] }).success? # => false
puts BrokenSchema.call({ foo: [{ bar: "bar" }] }).errors.to_h # => {:foo=>{0=>{:bar=>["is not allowed"]}}}

puts WorkingSchema.call({ foo: [{ bar: "bar" }], oof: [{ bar: "bar" }] }).success? # => true
puts WorkingSchema.call({ foo: [{ bar: "bar" }], oof: [{ bar: "bar" }] }).errors.to_h # => {}

Expected behavior

I would expect using a predefined schema on a "maybe" array would allow valid keys. Please note that when using filled(:array), things do work as expected.

I may be misunderstanding either how maybe is supposed to work or how reusing schemas is supposed to work!

Your environment

  • Affects my production application: NO
  • Ruby version: 2.6.5
  • OS: 10.14.6
@adamransom adamransom added the bug label Jun 26, 2020
@solnic solnic added this to the 1.5.3 milestone Jul 9, 2020
@tadeusz-niemiec
Copy link

tadeusz-niemiec commented Aug 20, 2020

This is probably related to #308, I proposed a fix there

@solnic
Copy link
Member

solnic commented Aug 21, 2020

This was fixed via #309

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants