-
Notifications
You must be signed in to change notification settings - Fork 82
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
Fix StrictHash contract for extra keys #254
Conversation
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sort
is missing for ruby 1.8.7, but is there any point of supporting this version? Also what do you think about comparing arrays sizes instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can drop support for ruby 1.8.7, I like comparing the keys instead of array size though.
lib/contracts/builtin_contracts.rb
Outdated
@@ -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]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there any point of accessing contract_hash[key]
instead of using _v
from the iterator? (which should be renamed to contract
in such case)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope, that sounds like a good change to me
Thanks for this PR! I will check it shortly |
lib/contracts/builtin_contracts.rb
Outdated
@@ -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]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope, that sounds like a good change to me
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can drop support for ruby 1.8.7, I like comparing the keys instead of array size though.
Fixed. I didn't touched 1.8.7 support as I saw that someone else wanted to do that (#255 (comment)). |
can you merge master into this branch? ci should pass, and then I'll merge |
`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.
`all?` iterator already provides contract for the `valid?` check. Use it instead of accessing `contract_hash` again.
@egonSchiele done. |
Thank you! |
## v0.16.0 - **Support for Ruby 1.8 has been discontinued** - [Corey Farwell](https://github.com/frewsxcv) [#256](egonSchiele/contracts.ruby#256) - Enhancement: Add a `Contracts::Attrs` module containing attribute w/ contracts utilities - [Corey Farwell](https://github.com/frewsxcv) [#255](egonSchiele/contracts.ruby#255) - Bugfix: Fix StrictHash contract for extra keys - [Maciej Malecki](https://github.com/smt116) [#254](egonSchiele/contracts.ruby#254) ## v0.15.0 - Bugfix: Func contract's return value isn't enforced with blocks - [Piotr Szmielew](https://github.com/esse) [#251](egonSchiele/contracts.ruby#251) - Bugfx: Fix contracts used in AR-models - [Gert Goet](https://github.com/eval) [#237](egonSchiele/contracts.ruby#237)
Contracts::StrictHash
didn't complain about extra entries in a givenhash. 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.