Skip to content

Unexpected key validation with schema composition #444

@masterT

Description

@masterT

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions