Skip to content

Commit

Permalink
Fix StrictHash contract for extra keys
Browse files Browse the repository at this point in the history
`Contracts::StrictHash` didn't complain about extra entries in a given
hash. This was because of the missing assertion for keys. Specs hadn't
caught this case because `age` key had a wrong type anyway.

Compare keys for contract and a given hash and return false if they are
equal.
  • Loading branch information
smt116 committed Apr 20, 2017
1 parent 8e6d4ee commit 89b9ca2
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/contracts/builtin_contracts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ def initialize(contract_hash)

def valid?(arg)
return false unless arg.is_a?(Hash)
return false unless arg.keys.sort.eql?(contract_hash.keys.sort)

contract_hash.all? do |key, _v|
contract_hash.key?(key) && Contract.valid?(arg[key], contract_hash[key])
Expand Down
2 changes: 1 addition & 1 deletion spec/builtin_contracts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ def something(hash)

context "when given an input with extra keys" do
it "raises an error" do
fails { @o.strict_person(:name => "calvin", :age => "10", :soft => true) }
fails { @o.strict_person(:name => "calvin", :age => 10, :soft => true) }
end
end

Expand Down

0 comments on commit 89b9ca2

Please sign in to comment.