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

Fix StrictHash contract for extra keys #254

Merged
merged 2 commits into from
Apr 20, 2017
Merged

Conversation

smt116
Copy link
Contributor

@smt116 smt116 commented Apr 7, 2017

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.

@@ -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)
Copy link
Contributor Author

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?

Copy link
Owner

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.

@@ -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])
Copy link
Contributor Author

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)

Copy link
Owner

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

@egonSchiele
Copy link
Owner

Thanks for this PR! I will check it shortly

@@ -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])
Copy link
Owner

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)
Copy link
Owner

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.

@smt116
Copy link
Contributor Author

smt116 commented Apr 18, 2017

Fixed. I didn't touched 1.8.7 support as I saw that someone else wanted to do that (#255 (comment)).

@egonSchiele
Copy link
Owner

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.
@smt116
Copy link
Contributor Author

smt116 commented Apr 20, 2017

@egonSchiele done.

@egonSchiele egonSchiele merged commit c408bcc into egonSchiele:master Apr 20, 2017
@egonSchiele
Copy link
Owner

Thank you!

jsonn pushed a commit to jsonn/pkgsrc that referenced this pull request Jun 4, 2017
## 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants