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

Unexpected key validation with schema composition #444

Open
masterT opened this issue Nov 23, 2022 · 2 comments
Open

Unexpected key validation with schema composition #444

masterT opened this issue Nov 23, 2022 · 2 comments

Comments

@masterT
Copy link

masterT commented Nov 23, 2022

Describe the bug

When using schema composition or in a schema along with config.validate_keys = true, the errors for the keys are set considering all the composed schemas.

To Reproduce

require 'dry-schema'

strategy_foo_schema = Dry::Schema.define do
  required(:type).filled(:string, eql?: 'foo')
  required(:options).hash do
    required(:foo).filled(:string)
  end
end

strategy_bar_schema = Dry::Schema.define do
  required(:type).filled(:string, eql?: 'bar')
  required(:options).hash do
    required(:bar).filled(:string)
  end
end

configuration_schema = Dry::Schema.define do
  config.validate_keys = true

  required(:strategy).schema(strategy_foo_schema | strategy_bar_schema)
end

input = {
  strategy: {
    type: 'foo',
    options: {
      foo: 'foo'
    }
  }
}

puts configuration_schema.call(input)&.errors&.to_h
# => {:strategy=>{:options=>{:foo=>["is not allowed"]}}}

Expected behavior

I would expect the key validation to only apply to the matching schema of the composed schema.

My environment

  • Affects my production application: NO
  • Ruby version: v2.7.6
  • OS: macOS Monterey 12.4
@solnic
Copy link
Member

solnic commented Nov 26, 2022

Is it still an issue with dry-schema 1.13.0?

@masterT
Copy link
Author

masterT commented Nov 27, 2022

Is it still an issue with dry-schema 1.13.0?

Yes, it it still.

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