Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop yielding skip value #2341

Merged
merged 5 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [#2331](https://github.com/ruby-grape/grape/pull/2331): Memory optimization when running validators - [@ericproulx](https://github.com/ericproulx).
* [#2332](https://github.com/ruby-grape/grape/pull/2332): Use ActiveSupport configurable - [@ericproulx](https://github.com/ericproulx).
* [#2333](https://github.com/ruby-grape/grape/pull/2333): Use custom messages in parameter validation with arity 1 - [@thedevjoao](https://github.com/TheDevJoao).
* [#2341](https://github.com/ruby-grape/grape/pull/2341): Stop yielding skip value - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

#### Fixes
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/multiple_attributes_iterator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class MultipleAttributesIterator < AttributesIterator
private

def yield_attributes(resource_params, _attrs)
yield resource_params, skip?(resource_params)
yield resource_params unless skip?(resource_params)
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/grape/validations/single_attribute_iterator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ class SingleAttributeIterator < AttributesIterator
private

def yield_attributes(val, attrs)
return if skip?(val)

attrs.each do |attr_name|
yield val, attr_name, empty?(val), skip?(val)
yield val, attr_name, empty?(val)
end
end

Expand Down
11 changes: 4 additions & 7 deletions lib/grape/validations/validators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,13 @@ def validate!(params)
# there may be more than one error per field
array_errors = []

attributes.each do |val, attr_name, empty_val, skip_value|
next if skip_value
attributes.each do |val, attr_name, empty_val|
next if [email protected]? && empty_val
next unless @scope.meets_dependency?(val, params)

begin
validate_param!(attr_name, val) if @required || (val.respond_to?(:key?) && val.key?(attr_name))
rescue Grape::Exceptions::Validation => e
array_errors << e
end
validate_param!(attr_name, val) if @required || (val.respond_to?(:key?) && val.key?(attr_name))
rescue Grape::Exceptions::Validation => e
array_errors << e
end

raise Grape::Exceptions::ValidationArrayErrors.new(array_errors) if array_errors.any?
Expand Down
12 changes: 4 additions & 8 deletions lib/grape/validations/validators/multiple_params_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ def validate!(params)
attributes = MultipleAttributesIterator.new(self, @scope, params)
array_errors = []

attributes.each do |resource_params, skip_value|
next if skip_value

begin
validate_params!(resource_params)
rescue Grape::Exceptions::Validation => e
array_errors << e
end
attributes.each do |resource_params|
validate_params!(resource_params)
rescue Grape::Exceptions::Validation => e
array_errors << e
end

raise Grape::Exceptions::ValidationArrayErrors.new(array_errors) if array_errors.any?
Expand Down
14 changes: 6 additions & 8 deletions spec/grape/validations/multiple_attributes_iterator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
{ first: 'string', second: 'string' }
end

it 'yields the whole params hash and the skipped flag without the list of attrs' do
expect { |b| iterator.each(&b) }.to yield_with_args(params, false)
it 'yields the whole params hash without the list of attrs' do
expect { |b| iterator.each(&b) }.to yield_with_args(params)
end
end

Expand All @@ -23,17 +23,15 @@
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], false], [params[1], false])
expect { |b| iterator.each(&b) }.to yield_successive_args(params[0], params[1])
end
end

context 'when params is empty optional placeholder' do
let(:params) do
[Grape::DSL::Parameters::EmptyOptionalValue, { first: 'string2', second: 'string2' }]
end
let(:params) { [Grape::DSL::Parameters::EmptyOptionalValue] }

it 'yields each element of the array without the list of attrs' do
expect { |b| iterator.each(&b) }.to yield_successive_args([Grape::DSL::Parameters::EmptyOptionalValue, true], [params[1], false])
it 'does not yield it' do
expect { |b| iterator.each(&b) }.to yield_successive_args
end
end
end
Expand Down
17 changes: 8 additions & 9 deletions spec/grape/validations/single_attribute_iterator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

it 'yields params and every single attribute from the list' do
expect { |b| iterator.each(&b) }
.to yield_successive_args([params, :first, false, false], [params, :second, false, false])
.to yield_successive_args([params, :first, false], [params, :second, false])
end
end

Expand All @@ -25,8 +25,8 @@

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, false, false], [params[0], :second, false, false],
[params[1], :first, false, false], [params[1], :second, false, false]
[params[0], :first, false], [params[0], :second, false],
[params[1], :first, false], [params[1], :second, false]
)
end

Expand All @@ -35,20 +35,19 @@

it 'marks params with empty values' do
expect { |b| iterator.each(&b) }.to yield_successive_args(
[params[0], :first, true, false], [params[0], :second, true, false],
[params[1], :first, true, false], [params[1], :second, true, false],
[params[2], :first, false, false], [params[2], :second, false, false]
[params[0], :first, true], [params[0], :second, true],
[params[1], :first, true], [params[1], :second, true],
[params[2], :first, false], [params[2], :second, false]
)
end
end

context 'when missing optional value' do
let(:params) { [Grape::DSL::Parameters::EmptyOptionalValue, 10] }

it 'marks params with skipped values' do
it 'does not yield skipped values' do
expect { |b| iterator.each(&b) }.to yield_successive_args(
[params[0], :first, false, true], [params[0], :second, false, true],
[params[1], :first, false, false], [params[1], :second, false, false]
[params[1], :first, false], [params[1], :second, false]
)
end
end
Expand Down