-
-
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 #1913 from bikolya/fix-errors-for-multiple-params-…
…validations Fix error messages for nested multiple params validations
- Loading branch information
Showing
20 changed files
with
848 additions
and
232 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Grape | ||
module Validations | ||
class MultipleAttributesIterator < AttributesIterator | ||
private | ||
|
||
def yield_attributes(resource_params, _attrs) | ||
yield resource_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Grape | ||
module Validations | ||
class SingleAttributeIterator < AttributesIterator | ||
private | ||
|
||
def yield_attributes(resource_params, attrs) | ||
attrs.each do |attr_name| | ||
yield resource_params, attr_name | ||
end | ||
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
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
29 changes: 29 additions & 0 deletions
29
spec/grape/validations/multiple_attributes_iterator_spec.rb
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,29 @@ | ||
require 'spec_helper' | ||
|
||
describe Grape::Validations::MultipleAttributesIterator do | ||
describe '#each' do | ||
subject(:iterator) { described_class.new(validator, scope, params) } | ||
let(:scope) { Grape::Validations::ParamsScope.new(api: Class.new(Grape::API)) } | ||
let(:validator) { double(attrs: %i[first second third]) } | ||
|
||
context 'when params is a hash' do | ||
let(:params) do | ||
{ first: 'string', second: 'string' } | ||
end | ||
|
||
it 'yields the whole params hash without the list of attrs' do | ||
expect { |b| iterator.each(&b) }.to yield_with_args(params) | ||
end | ||
end | ||
|
||
context 'when params is an array' do | ||
let(:params) do | ||
[{ first: 'string1', second: 'string1' }, { first: 'string2', second: 'string2' }] | ||
end | ||
|
||
it 'yields each element of the array without the list of attrs' do | ||
expect { |b| iterator.each(&b) }.to yield_successive_args(params[0], params[1]) | ||
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,33 @@ | ||
require 'spec_helper' | ||
|
||
describe Grape::Validations::SingleAttributeIterator do | ||
describe '#each' do | ||
subject(:iterator) { described_class.new(validator, scope, params) } | ||
let(:scope) { Grape::Validations::ParamsScope.new(api: Class.new(Grape::API)) } | ||
let(:validator) { double(attrs: %i[first second third]) } | ||
|
||
context 'when params is a hash' do | ||
let(:params) do | ||
{ first: 'string', second: 'string' } | ||
end | ||
|
||
it 'yields params and every single attribute from the list' do | ||
expect { |b| iterator.each(&b) } | ||
.to yield_successive_args([params, :first], [params, :second], [params, :third]) | ||
end | ||
end | ||
|
||
context 'when params is an array' do | ||
let(:params) do | ||
[{ first: 'string1', second: 'string1' }, { first: 'string2', second: 'string2' }] | ||
end | ||
|
||
it 'yields every single attribute from the list for each of the array elements' do | ||
expect { |b| iterator.each(&b) }.to yield_successive_args( | ||
[params[0], :first], [params[0], :second], [params[0], :third], | ||
[params[1], :first], [params[1], :second], [params[1], :third] | ||
) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.