Skip to content

Fixed validation of invalid params with mutual_exclusive inside hash #1017

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

Merged
merged 1 commit into from
Jun 4, 2015
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* [#1005](https://github.com/intridea/grape/pull/1005): Fixed the Grape::Middleware::Globals - [@urkle](https://github.com/urkle).
* [#1012](https://github.com/intridea/grape/pull/1012): Fixed `allow_blank: false` with a Boolean value of `false` - [@mfunaro](https://github.com/mfunaro).
* [#1023](https://github.com/intridea/grape/issues/1023): Fixes unexpected beahvior with `present` and an object that responds to `merge` but isn't a Hash - [@dblock](https://github.com/dblock).
* [#1017](https://github.com/intridea/grape/pull/1017): Fixed `undefined method stringify_keys` with nested mutual exclusive params - [@quickpay](https://github.com/quickpay).
* Your contribution here.

0.11.0 (2/23/2015)
Expand Down
1 change: 1 addition & 0 deletions lib/grape/validations/validators/multiple_params_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def scope_requires_params
end

def keys_in_common(resource_params)
return [] unless resource_params.is_a?(Hash)
(all_keys & resource_params.stringify_keys.keys).map(&:to_s)
end

Expand Down
19 changes: 19 additions & 0 deletions spec/grape/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,25 @@ module SharedParams
expect(last_response.status).to eq(400)
end
end

context 'mutually exclusive params inside Hash group' do
it 'invalidates if request param is invalid type' do
subject.params do
optional :wine, type: Hash do
optional :grape
optional :country
mutually_exclusive :grape, :country
end
end
subject.post '/mutually_exclusive' do
'mutually_exclusive works!'
end

post '/mutually_exclusive', wine: '2015 sauvignon'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq 'wine is invalid'
end
end
end

context 'exactly one of' do
Expand Down