Skip to content

Commit

Permalink
Merge pull request #1987 from ZeroInputCtrl/exactly_one_spec
Browse files Browse the repository at this point in the history
Re-add exactly_one_of mutually exclusive error message
  • Loading branch information
dblock committed Feb 4, 2020
2 parents 8e0b232 + ad417aa commit b9d53a2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

#### Fixes

* [#1987](https://github.com/ruby-grape/grape/pull/1987): Re-add exactly_one_of mutually exclusive error message - [@ZeroInputCtrl](https://github.com/ZeroInputCtrl).
* [#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).
* [#1976](https://github.com/ruby-grape/grape/pull/1976): Ensure classes/modules listed for autoload really exist - [@dnesteryuk](https://github.com/dnesteryuk).
* [#1971](https://github.com/ruby-grape/grape/pull/1971): Fix BigDecimal coercion - [@FlickStuart](https://github.com/FlickStuart).
Expand Down
6 changes: 4 additions & 2 deletions lib/grape/validations/validators/exactly_one_of.rb
Expand Up @@ -6,8 +6,10 @@ module Grape
module Validations
class ExactlyOneOfValidator < MultipleParamsBase
def validate_params!(params)
return if keys_in_common(params).length == 1
raise Grape::Exceptions::Validation.new(params: all_keys, message: message(:exactly_one))
keys = keys_in_common(params)
return if keys.length == 1
raise Grape::Exceptions::Validation.new(params: all_keys, message: message(:exactly_one)) if keys.length.zero?
raise Grape::Exceptions::Validation.new(params: keys, message: message(:mutual_exclusion))
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/grape/exceptions/validation_errors_spec.rb
Expand Up @@ -81,8 +81,8 @@ def app
expect(last_response.status).to eq(400)
expect(JSON.parse(last_response.body)).to eq(
[
'params' => %w[beer wine juice],
'messages' => ['are missing, exactly one parameter must be provided']
'params' => %w[beer wine],
'messages' => ['are mutually exclusive']
]
)
end
Expand Down
24 changes: 12 additions & 12 deletions spec/grape/validations/validators/exactly_one_of_spec.rb
Expand Up @@ -100,7 +100,7 @@ def app
validate
expect(last_response.status).to eq 400
expect(JSON.parse(last_response.body)).to eq(
'beer,wine,grapefruit' => ['are missing, exactly one parameter must be provided']
'beer,wine,grapefruit' => ['are mutually exclusive']
)
end

Expand All @@ -112,7 +112,7 @@ def app
validate
expect(last_response.status).to eq 400
expect(JSON.parse(last_response.body)).to eq(
'beer,wine,grapefruit' => ['are missing, exactly one parameter must be provided']
'beer,wine,grapefruit' => ['are mutually exclusive']
)
end
end
Expand All @@ -126,7 +126,7 @@ def app
validate
expect(last_response.status).to eq 400
expect(JSON.parse(last_response.body)).to eq(
'beer,wine,grapefruit' => ['are missing, exactly one parameter must be provided']
'beer,grapefruit' => ['are mutually exclusive']
)
end
end
Expand All @@ -139,7 +139,7 @@ def app
validate
expect(last_response.status).to eq 400
expect(JSON.parse(last_response.body)).to eq(
'beer,wine,grapefruit' => ['you should choose one']
'beer,wine' => ['you should choose one']
)
end
end
Expand Down Expand Up @@ -175,7 +175,7 @@ def app
validate
expect(last_response.status).to eq 400
expect(JSON.parse(last_response.body)).to eq(
'item[beer],item[wine],item[grapefruit]' => ['are missing, exactly one parameter must be provided']
'item[beer],item[wine]' => ['are mutually exclusive']
)
end
end
Expand All @@ -190,7 +190,7 @@ def app
validate
expect(last_response.status).to eq 400
expect(JSON.parse(last_response.body)).to eq(
'item[beer],item[wine],item[grapefruit]' => ['are missing, exactly one parameter must be provided']
'item[beer],item[wine]' => ['are mutually exclusive']
)
end
end
Expand All @@ -213,11 +213,11 @@ def app
validate
expect(last_response.status).to eq 400
expect(JSON.parse(last_response.body)).to eq(
'items[0][beer],items[0][wine],items[0][grapefruit]' => [
'are missing, exactly one parameter must be provided'
'items[0][beer],items[0][wine]' => [
'are mutually exclusive'
],
'items[1][beer],items[1][wine],items[1][grapefruit]' => [
'are missing, exactly one parameter must be provided'
'items[1][wine],items[1][grapefruit]' => [
'are mutually exclusive'
]
)
end
Expand All @@ -231,8 +231,8 @@ def app
validate
expect(last_response.status).to eq 400
expect(JSON.parse(last_response.body)).to eq(
'items[0][nested_items][0][beer],items[0][nested_items][0][wine],items[0][nested_items][0][grapefruit]' => [
'are missing, exactly one parameter must be provided'
'items[0][nested_items][0][beer],items[0][nested_items][0][wine]' => [
'are mutually exclusive'
]
)
end
Expand Down
6 changes: 3 additions & 3 deletions spec/grape/validations_spec.rb
Expand Up @@ -1418,7 +1418,7 @@ def validate_param!(attr_name, params)
it 'errors when two or more are present' do
get '/custom_message/exactly_one_of', beer: 'string', wine: 'anotherstring'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq 'beer, wine, juice are missing, exactly one parameter is required'
expect(last_response.body).to eq 'beer, wine are missing, exactly one parameter is required'
end
end

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

Expand Down Expand Up @@ -1477,7 +1477,7 @@ def validate_param!(attr_name, params)
it 'errors when two or more are present' do
get '/exactly_one_of_nested', nested: { beer_nested: 'string' }, nested2: [{ beer_nested2: 'string', wine_nested2: 'anotherstring' }]
expect(last_response.status).to eq(400)
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'
expect(last_response.body).to eq 'nested2[0][beer_nested2], nested2[0][wine_nested2] are mutually exclusive'
end
end
end
Expand Down

0 comments on commit b9d53a2

Please sign in to comment.