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

Raise an InvalidKeyErrors on substring of valid keys #706

Merged
merged 3 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/dry/validation/contract/class_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,13 @@ def ensure_valid_keys(*keys)
valid_paths = key_map.to_dot_notation
key_paths = key_paths(keys)

invalid_keys = key_paths.map { |(key, path)|
unless valid_paths.any? { |vp| vp.include?(path) || vp.include?("#{path}[]") }
invalid_keys = key_paths.filter_map { |(key, path)|
if valid_paths.none? { |vp|
vp == path || vp.start_with?("#{path}.", "#{path}[]")
}
key
end
}.compact.uniq
}.uniq

return if invalid_keys.empty?

Expand Down
68 changes: 68 additions & 0 deletions spec/integration/contract/class_interface/rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,74 @@ def self.name
end
end

context "when keys are prefixes of valid keys" do
it "raises error with a list of symbol keys" do
expect { contract_class.rule(:details, :addres) }
.to raise_error(
Dry::Validation::InvalidKeysError,
"TestContract.rule specifies keys that are not defined by the schema: [:addres]"
)
end

it "raises error with a hash path" do
expect { contract_class.rule(details: :addres) }
.to raise_error(
Dry::Validation::InvalidKeysError,
"TestContract.rule specifies keys that are not defined by the schema: [{:details=>:addres}]"
)
end

it "raises error with a dot notation" do
expect { contract_class.rule("details.addres") }
.to raise_error(
Dry::Validation::InvalidKeysError,
'TestContract.rule specifies keys that are not defined by the schema: ["details.addres"]'
)
end

it "raises error with a hash path with multiple nested keys" do
expect { contract_class.rule(details: %i[addres]) }
.to raise_error(
Dry::Validation::InvalidKeysError,
"TestContract.rule specifies keys that are not defined by the schema: [{:details=>[:addres]}]"
)
end
end

context "when keys are suffixes of valid keys" do
it "raises error with a list of symbol keys" do
expect { contract_class.rule(:etails, :address) }
.to raise_error(
Dry::Validation::InvalidKeysError,
"TestContract.rule specifies keys that are not defined by the schema: [:etails, :address]"
)
end

it "raises error with a hash path" do
expect { contract_class.rule(etails: :address) }
.to raise_error(
Dry::Validation::InvalidKeysError,
"TestContract.rule specifies keys that are not defined by the schema: [{:etails=>:address}]"
)
end

it "raises error with a dot notation" do
expect { contract_class.rule("etails.address") }
.to raise_error(
Dry::Validation::InvalidKeysError,
'TestContract.rule specifies keys that are not defined by the schema: ["etails.address"]'
)
end

it "raises error with a hash path with multiple nested keys" do
expect { contract_class.rule(etails: %i[address]) }
.to raise_error(
Dry::Validation::InvalidKeysError,
"TestContract.rule specifies keys that are not defined by the schema: [{:etails=>[:address]}]"
)
end
end

describe "abstract contract" do
let(:abstract_contract) do
Class.new(Dry::Validation::Contract) do
Expand Down