Skip to content

Commit 4088745

Browse files
committed
Fixed validation of invalid params with mutual_exclusive inside hash
1 parent 2ec51f7 commit 4088745

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* [#1005](https://github.com/intridea/grape/pull/1005): Fixed the Grape::Middleware::Globals - [@urkle](https://github.com/urkle).
2424
* [#1012](https://github.com/intridea/grape/pull/1012): Fixed `allow_blank: false` with a Boolean value of `false` - [@mfunaro](https://github.com/mfunaro).
2525
* [#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).
26+
* [#1017](https://github.com/intridea/grape/pull/1017): Fixed `undefined method stringify_keys` with nested mutual exclusive params - [@quickpay](https://github.com/quickpay).
2627
* Your contribution here.
2728

2829
0.11.0 (2/23/2015)

lib/grape/validations/validators/multiple_params_base.rb

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def scope_requires_params
1515
end
1616

1717
def keys_in_common(resource_params)
18+
return [] unless resource_params.is_a?(Hash)
1819
(all_keys & resource_params.stringify_keys.keys).map(&:to_s)
1920
end
2021

spec/grape/validations_spec.rb

+19
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,25 @@ module SharedParams
11321132
expect(last_response.status).to eq(400)
11331133
end
11341134
end
1135+
1136+
context 'mutually exclusive params inside Hash group' do
1137+
it 'invalidates if request param is invalid type' do
1138+
subject.params do
1139+
optional :wine, type: Hash do
1140+
optional :grape
1141+
optional :country
1142+
mutually_exclusive :grape, :country
1143+
end
1144+
end
1145+
subject.post '/mutually_exclusive' do
1146+
'mutually_exclusive works!'
1147+
end
1148+
1149+
post '/mutually_exclusive', wine: '2015 sauvignon'
1150+
expect(last_response.status).to eq(400)
1151+
expect(last_response.body).to eq 'wine is invalid'
1152+
end
1153+
end
11351154
end
11361155

11371156
context 'exactly one of' do

0 commit comments

Comments
 (0)