-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #626 from oliverbarnes/mutually_exclusive
Add mutually_exclusive params validation
- Loading branch information
Showing
9 changed files
with
161 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Grape | ||
module Validations | ||
class MutualExclusionValidator < Validator | ||
attr_reader :params | ||
|
||
def validate!(params) | ||
@params = params | ||
if two_or_more_exclusive_params_are_present | ||
raise Grape::Exceptions::Validation, param: "#{keys_in_common.map(&:to_sym)}", message_key: :mutual_exclusion | ||
end | ||
params | ||
end | ||
|
||
private | ||
|
||
def two_or_more_exclusive_params_are_present | ||
keys_in_common.length > 1 | ||
end | ||
|
||
def keys_in_common | ||
attrs.map(&:to_s) & params.stringify_keys.keys | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'spec_helper' | ||
|
||
describe Grape::Validations::MutualExclusionValidator do | ||
describe '#validate!' do | ||
let(:scope) do | ||
Struct.new(:opts) do | ||
def params(arg); end | ||
end | ||
end | ||
let(:mutually_exclusive_params) { [:beer, :wine, :grapefruit] } | ||
let(:validator) { described_class.new(mutually_exclusive_params, {}, false, scope.new) } | ||
|
||
context 'when all mutually exclusive params are present' do | ||
let(:params) { { beer: true, wine: true, grapefruit: true } } | ||
|
||
it 'raises a validation exception' do | ||
expect { | ||
validator.validate! params | ||
}.to raise_error(Grape::Exceptions::Validation) | ||
end | ||
|
||
context 'mixed with other params' do | ||
let(:mixed_params) { params.merge!(other: true, andanother: true) } | ||
|
||
it 'still raises a validation exception' do | ||
expect { | ||
validator.validate! mixed_params | ||
}.to raise_error(Grape::Exceptions::Validation) | ||
end | ||
end | ||
end | ||
|
||
context 'when a subset of mutually exclusive params are present' do | ||
let(:params) { { beer: true, grapefruit: true } } | ||
|
||
it 'raises a validation exception' do | ||
expect { | ||
validator.validate! params | ||
}.to raise_error(Grape::Exceptions::Validation) | ||
end | ||
end | ||
|
||
context 'when params keys come as strings' do | ||
let(:params) { { 'beer' => true, 'grapefruit' => true } } | ||
|
||
it 'raises a validation exception' do | ||
expect { | ||
validator.validate! params | ||
}.to raise_error(Grape::Exceptions::Validation) | ||
end | ||
end | ||
|
||
context 'when no mutually exclusive params are present' do | ||
let(:params) { { beer: true, somethingelse: true } } | ||
|
||
it 'params' do | ||
expect(validator.validate!(params)).to eql params | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters