Skip to content

Commit

Permalink
Fix duped keys in key coercer and validator
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Sep 21, 2022
1 parent f29881d commit 5de054a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/dry/schema/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def parent_filter_schemas
#
# @api private
def key_validator
KeyValidator.new(key_map: key_map + parent_key_map)
KeyValidator.new(key_map: key_map)
end

# Build a key coercer
Expand All @@ -446,7 +446,7 @@ def key_validator
#
# @api private
def key_coercer
KeyCoercer.symbolized(key_map + parent_key_map)
KeyCoercer.symbolized(key_map)
end

# Build a value coercer
Expand Down
20 changes: 20 additions & 0 deletions spec/integration/schema/steps_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,24 @@
}.to raise_error(ArgumentError, /oops/)
end
end

context "with an inherited params" do
describe "core step key maps" do
it "copies key map from the parent and includes new keys from child" do
parent = Dry::Schema.Params do
config.validate_keys = true

required(:name).filled(:string)
end

child = Dry::Schema.Params(parent: parent) do
required(:email).filled(:string)
end

%i[key_validator key_coercer].each do |step|
expect(child.steps[step].executor.key_map.map(&:name)).to eql(%w[name email])
end
end
end
end
end
17 changes: 17 additions & 0 deletions spec/integration/schema/unexpected_keys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,21 @@
end
end
end

context "with an inherited params" do
it "copies key map from the parent and includes new keys from child" do
parent = Dry::Schema.Params do
config.validate_keys = true

required(:name).filled(:string)
end

child = Dry::Schema.Params(parent: parent) do
required(:email).filled(:string)
end

expect(child.(name: "Jane", email: "[email protected]", unexpected_key: "boo!").errors.to_h)
.to eql({unexpected_key: ["is not allowed"]})
end
end
end

0 comments on commit 5de054a

Please sign in to comment.