Skip to content

Commit

Permalink
Merge bde1ca9 into 8e0b232
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroInputCtrl committed Feb 4, 2020
2 parents 8e0b232 + bde1ca9 commit 9157f06
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
3 changes: 2 additions & 1 deletion lib/grape/validations/validators/exactly_one_of.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ 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))
raise Grape::Exceptions::Validation.new(params: all_keys, message: message(:exactly_one)) if keys_in_common(params).length.zero?
raise Grape::Exceptions::Validation.new(params: keys_in_common(params), message: message(:mutual_exclusion))
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/grape/exceptions/validation_errors_spec.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 9157f06

Please sign in to comment.