Skip to content

Commit b9d53a2

Browse files
authored
Merge pull request #1987 from ZeroInputCtrl/exactly_one_spec
Re-add exactly_one_of mutually exclusive error message
2 parents 8e0b232 + ad417aa commit b9d53a2

File tree

5 files changed

+22
-19
lines changed

5 files changed

+22
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#### Fixes
88

9+
* [#1987](https://github.com/ruby-grape/grape/pull/1987): Re-add exactly_one_of mutually exclusive error message - [@ZeroInputCtrl](https://github.com/ZeroInputCtrl).
910
* [#1977](https://github.com/ruby-grape/grape/pull/1977): Skip validation for a file if it is optional and nil - [@dnesteryuk](https://github.com/dnesteryuk).
1011
* [#1976](https://github.com/ruby-grape/grape/pull/1976): Ensure classes/modules listed for autoload really exist - [@dnesteryuk](https://github.com/dnesteryuk).
1112
* [#1971](https://github.com/ruby-grape/grape/pull/1971): Fix BigDecimal coercion - [@FlickStuart](https://github.com/FlickStuart).

lib/grape/validations/validators/exactly_one_of.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ module Grape
66
module Validations
77
class ExactlyOneOfValidator < MultipleParamsBase
88
def validate_params!(params)
9-
return if keys_in_common(params).length == 1
10-
raise Grape::Exceptions::Validation.new(params: all_keys, message: message(:exactly_one))
9+
keys = keys_in_common(params)
10+
return if keys.length == 1
11+
raise Grape::Exceptions::Validation.new(params: all_keys, message: message(:exactly_one)) if keys.length.zero?
12+
raise Grape::Exceptions::Validation.new(params: keys, message: message(:mutual_exclusion))
1113
end
1214
end
1315
end

spec/grape/exceptions/validation_errors_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ def app
8181
expect(last_response.status).to eq(400)
8282
expect(JSON.parse(last_response.body)).to eq(
8383
[
84-
'params' => %w[beer wine juice],
85-
'messages' => ['are missing, exactly one parameter must be provided']
84+
'params' => %w[beer wine],
85+
'messages' => ['are mutually exclusive']
8686
]
8787
)
8888
end

spec/grape/validations/validators/exactly_one_of_spec.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def app
100100
validate
101101
expect(last_response.status).to eq 400
102102
expect(JSON.parse(last_response.body)).to eq(
103-
'beer,wine,grapefruit' => ['are missing, exactly one parameter must be provided']
103+
'beer,wine,grapefruit' => ['are mutually exclusive']
104104
)
105105
end
106106

@@ -112,7 +112,7 @@ def app
112112
validate
113113
expect(last_response.status).to eq 400
114114
expect(JSON.parse(last_response.body)).to eq(
115-
'beer,wine,grapefruit' => ['are missing, exactly one parameter must be provided']
115+
'beer,wine,grapefruit' => ['are mutually exclusive']
116116
)
117117
end
118118
end
@@ -126,7 +126,7 @@ def app
126126
validate
127127
expect(last_response.status).to eq 400
128128
expect(JSON.parse(last_response.body)).to eq(
129-
'beer,wine,grapefruit' => ['are missing, exactly one parameter must be provided']
129+
'beer,grapefruit' => ['are mutually exclusive']
130130
)
131131
end
132132
end
@@ -139,7 +139,7 @@ def app
139139
validate
140140
expect(last_response.status).to eq 400
141141
expect(JSON.parse(last_response.body)).to eq(
142-
'beer,wine,grapefruit' => ['you should choose one']
142+
'beer,wine' => ['you should choose one']
143143
)
144144
end
145145
end
@@ -175,7 +175,7 @@ def app
175175
validate
176176
expect(last_response.status).to eq 400
177177
expect(JSON.parse(last_response.body)).to eq(
178-
'item[beer],item[wine],item[grapefruit]' => ['are missing, exactly one parameter must be provided']
178+
'item[beer],item[wine]' => ['are mutually exclusive']
179179
)
180180
end
181181
end
@@ -190,7 +190,7 @@ def app
190190
validate
191191
expect(last_response.status).to eq 400
192192
expect(JSON.parse(last_response.body)).to eq(
193-
'item[beer],item[wine],item[grapefruit]' => ['are missing, exactly one parameter must be provided']
193+
'item[beer],item[wine]' => ['are mutually exclusive']
194194
)
195195
end
196196
end
@@ -213,11 +213,11 @@ def app
213213
validate
214214
expect(last_response.status).to eq 400
215215
expect(JSON.parse(last_response.body)).to eq(
216-
'items[0][beer],items[0][wine],items[0][grapefruit]' => [
217-
'are missing, exactly one parameter must be provided'
216+
'items[0][beer],items[0][wine]' => [
217+
'are mutually exclusive'
218218
],
219-
'items[1][beer],items[1][wine],items[1][grapefruit]' => [
220-
'are missing, exactly one parameter must be provided'
219+
'items[1][wine],items[1][grapefruit]' => [
220+
'are mutually exclusive'
221221
]
222222
)
223223
end
@@ -231,8 +231,8 @@ def app
231231
validate
232232
expect(last_response.status).to eq 400
233233
expect(JSON.parse(last_response.body)).to eq(
234-
'items[0][nested_items][0][beer],items[0][nested_items][0][wine],items[0][nested_items][0][grapefruit]' => [
235-
'are missing, exactly one parameter must be provided'
234+
'items[0][nested_items][0][beer],items[0][nested_items][0][wine]' => [
235+
'are mutually exclusive'
236236
]
237237
)
238238
end

spec/grape/validations_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,7 @@ def validate_param!(attr_name, params)
14181418
it 'errors when two or more are present' do
14191419
get '/custom_message/exactly_one_of', beer: 'string', wine: 'anotherstring'
14201420
expect(last_response.status).to eq(400)
1421-
expect(last_response.body).to eq 'beer, wine, juice are missing, exactly one parameter is required'
1421+
expect(last_response.body).to eq 'beer, wine are missing, exactly one parameter is required'
14221422
end
14231423
end
14241424

@@ -1437,7 +1437,7 @@ def validate_param!(attr_name, params)
14371437
it 'errors when two or more are present' do
14381438
get '/exactly_one_of', beer: 'string', wine: 'anotherstring'
14391439
expect(last_response.status).to eq(400)
1440-
expect(last_response.body).to eq 'beer, wine, juice are missing, exactly one parameter must be provided'
1440+
expect(last_response.body).to eq 'beer, wine are mutually exclusive'
14411441
end
14421442
end
14431443

@@ -1477,7 +1477,7 @@ def validate_param!(attr_name, params)
14771477
it 'errors when two or more are present' do
14781478
get '/exactly_one_of_nested', nested: { beer_nested: 'string' }, nested2: [{ beer_nested2: 'string', wine_nested2: 'anotherstring' }]
14791479
expect(last_response.status).to eq(400)
1480-
expect(last_response.body).to eq 'nested2[0][beer_nested2], nested2[0][wine_nested2], nested2[0][juice_nested2] are missing, exactly one parameter must be provided'
1480+
expect(last_response.body).to eq 'nested2[0][beer_nested2], nested2[0][wine_nested2] are mutually exclusive'
14811481
end
14821482
end
14831483
end

0 commit comments

Comments
 (0)