Skip to content

Commit

Permalink
Merge pull request #430 from dry-rb/fix-duped-keys-in-key-validator
Browse files Browse the repository at this point in the history
[changelog]

fixed: "Key validator and coercer no longer include duped keys when using parent schemas (via #430) (@solnic)"
  • Loading branch information
solnic authored Sep 21, 2022
2 parents 81606f5 + 5de054a commit 663a43f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion 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 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 663a43f

Please sign in to comment.