Skip to content

Commit

Permalink
Merge pull request #711 from mereghost/676-wrong-index-query
Browse files Browse the repository at this point in the history
[changelog]

version: unreleased
fixed: "Duplicated keys mishandling in rule evaluation (issue #676 fixed via #711) (@mereghost)"
  • Loading branch information
solnic authored Jun 17, 2022
2 parents 224c289 + a4ac406 commit e65504e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/dry/validation/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ def error?(result, spec)

path
.to_a[0..-2]
.any? { |key|
curr_path = Schema::Path[path.keys[0..path.keys.index(key)]]
.each_with_index
.any? { |_key, index|
curr_path = Schema::Path[path.keys[0..index]]

return false unless result.schema_error?(curr_path)

Expand Down
40 changes: 40 additions & 0 deletions spec/integration/contract/call_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,44 @@ def self.name
expect(result).to be_failure
expect(result.errors.to_h).to eql(address: {geolocation: {lon: ["invalid longitude"]}})
end

context "duplicate key names on nested structures" do
subject(:contract) do
Class.new(Dry::Validation::Contract) do
def self.name
"RuleTestContract"
end

schema do
required(:data).hash do
required(:wrapper).hash do
required(:data).hash do
required(:id).filled(:string)
end
end
end
end

register_macro(:min_size) do |macro:|
min = macro.args[0]
key.failure("must have at least #{min} characters") unless value.size >= min
end

rule(%i[data wrapper data id]).validate(min_size: 10)
end.new
end

it "only applies the rules to" do
expected = {data: {wrapper: {data: ["must be a hash"]}}}
result = contract.(
data: {
wrapper: {
data: []
}
}
)

expect(result.errors.to_h).to eq(expected)
end
end
end

0 comments on commit e65504e

Please sign in to comment.