-
-
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.
Added all_or_none parameter validator.
- Loading branch information
1 parent
2a0da79
commit 58c17e9
Showing
6 changed files
with
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Grape | ||
module Validations | ||
require 'grape/validations/validators/multiple_params_base' | ||
class AllOrNoneOfValidator < MultipleParamsBase | ||
def validate!(params) | ||
super | ||
if scope_requires_params && only_subset_present | ||
raise Grape::Exceptions::Validation, params: all_keys, message_key: :all_or_none | ||
end | ||
params | ||
end | ||
|
||
private | ||
|
||
def only_subset_present | ||
scoped_params.any? { |resource_params| keys_in_common(resource_params).length > 0 && keys_in_common(resource_params).length < attrs.length } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'spec_helper' | ||
|
||
describe Grape::Validations::AllOrNoneOfValidator do | ||
describe '#validate!' do | ||
let(:scope) do | ||
Struct.new(:opts) do | ||
def params(arg) | ||
arg | ||
end | ||
|
||
def required?; end | ||
end | ||
end | ||
let(:all_or_none_params) { [:beer, :wine, :grapefruit] } | ||
let(:validator) { described_class.new(all_or_none_params, {}, false, scope.new) } | ||
|
||
context 'when all restricted params are present' do | ||
let(:params) { { beer: true, wine: true, grapefruit: true } } | ||
|
||
it 'does not raise a validation exception' do | ||
expect(validator.validate!(params)).to eql params | ||
end | ||
|
||
context 'mixed with other params' do | ||
let(:mixed_params) { params.merge!(other: true, andanother: true) } | ||
|
||
it 'does not raise a validation exception' do | ||
expect(validator.validate!(mixed_params)).to eql mixed_params | ||
end | ||
end | ||
end | ||
|
||
context 'when none of the restricted params is selected' do | ||
let(:params) { { somethingelse: true } } | ||
|
||
it 'does not raise a validation exception' do | ||
expect(validator.validate!(params)).to eql params | ||
end | ||
end | ||
|
||
context 'when only a subset of restricted 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 | ||
context 'mixed with other params' do | ||
let(:mixed_params) { params.merge!(other: true, andanother: true) } | ||
|
||
it 'raise a validation exception' do | ||
expect { | ||
validator.validate! params | ||
}.to raise_error(Grape::Exceptions::Validation) | ||
end | ||
end | ||
end | ||
|
||
end | ||
end |