Skip to content

Commit db7000b

Browse files
authored
Stop yielding skip value (#2341)
* Not yielding when skipping Adjust specs * Remove begin in rescue blocks * CHANGELOG entry * Fix typo
1 parent dd741b9 commit db7000b

7 files changed

+27
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* [#2331](https://github.com/ruby-grape/grape/pull/2331): Memory optimization when running validators - [@ericproulx](https://github.com/ericproulx).
99
* [#2332](https://github.com/ruby-grape/grape/pull/2332): Use ActiveSupport configurable - [@ericproulx](https://github.com/ericproulx).
1010
* [#2333](https://github.com/ruby-grape/grape/pull/2333): Use custom messages in parameter validation with arity 1 - [@thedevjoao](https://github.com/TheDevJoao).
11+
* [#2341](https://github.com/ruby-grape/grape/pull/2341): Stop yielding skip value - [@ericproulx](https://github.com/ericproulx).
1112
* Your contribution here.
1213

1314
#### Fixes

lib/grape/validations/multiple_attributes_iterator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class MultipleAttributesIterator < AttributesIterator
66
private
77

88
def yield_attributes(resource_params, _attrs)
9-
yield resource_params, skip?(resource_params)
9+
yield resource_params unless skip?(resource_params)
1010
end
1111
end
1212
end

lib/grape/validations/single_attribute_iterator.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ class SingleAttributeIterator < AttributesIterator
66
private
77

88
def yield_attributes(val, attrs)
9+
return if skip?(val)
10+
911
attrs.each do |attr_name|
10-
yield val, attr_name, empty?(val), skip?(val)
12+
yield val, attr_name, empty?(val)
1113
end
1214
end
1315

lib/grape/validations/validators/base.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,13 @@ def validate!(params)
4646
# there may be more than one error per field
4747
array_errors = []
4848

49-
attributes.each do |val, attr_name, empty_val, skip_value|
50-
next if skip_value
49+
attributes.each do |val, attr_name, empty_val|
5150
next if !@scope.required? && empty_val
5251
next unless @scope.meets_dependency?(val, params)
5352

54-
begin
55-
validate_param!(attr_name, val) if @required || (val.respond_to?(:key?) && val.key?(attr_name))
56-
rescue Grape::Exceptions::Validation => e
57-
array_errors << e
58-
end
53+
validate_param!(attr_name, val) if @required || (val.respond_to?(:key?) && val.key?(attr_name))
54+
rescue Grape::Exceptions::Validation => e
55+
array_errors << e
5956
end
6057

6158
raise Grape::Exceptions::ValidationArrayErrors.new(array_errors) if array_errors.any?

lib/grape/validations/validators/multiple_params_base.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@ def validate!(params)
88
attributes = MultipleAttributesIterator.new(self, @scope, params)
99
array_errors = []
1010

11-
attributes.each do |resource_params, skip_value|
12-
next if skip_value
13-
14-
begin
15-
validate_params!(resource_params)
16-
rescue Grape::Exceptions::Validation => e
17-
array_errors << e
18-
end
11+
attributes.each do |resource_params|
12+
validate_params!(resource_params)
13+
rescue Grape::Exceptions::Validation => e
14+
array_errors << e
1915
end
2016

2117
raise Grape::Exceptions::ValidationArrayErrors.new(array_errors) if array_errors.any?

spec/grape/validations/multiple_attributes_iterator_spec.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
{ first: 'string', second: 'string' }
1313
end
1414

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

@@ -23,17 +23,15 @@
2323
end
2424

2525
it 'yields each element of the array without the list of attrs' do
26-
expect { |b| iterator.each(&b) }.to yield_successive_args([params[0], false], [params[1], false])
26+
expect { |b| iterator.each(&b) }.to yield_successive_args(params[0], params[1])
2727
end
2828
end
2929

3030
context 'when params is empty optional placeholder' do
31-
let(:params) do
32-
[Grape::DSL::Parameters::EmptyOptionalValue, { first: 'string2', second: 'string2' }]
33-
end
31+
let(:params) { [Grape::DSL::Parameters::EmptyOptionalValue] }
3432

35-
it 'yields each element of the array without the list of attrs' do
36-
expect { |b| iterator.each(&b) }.to yield_successive_args([Grape::DSL::Parameters::EmptyOptionalValue, true], [params[1], false])
33+
it 'does not yield it' do
34+
expect { |b| iterator.each(&b) }.to yield_successive_args
3735
end
3836
end
3937
end

spec/grape/validations/single_attribute_iterator_spec.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

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

@@ -25,8 +25,8 @@
2525

2626
it 'yields every single attribute from the list for each of the array elements' do
2727
expect { |b| iterator.each(&b) }.to yield_successive_args(
28-
[params[0], :first, false, false], [params[0], :second, false, false],
29-
[params[1], :first, false, false], [params[1], :second, false, false]
28+
[params[0], :first, false], [params[0], :second, false],
29+
[params[1], :first, false], [params[1], :second, false]
3030
)
3131
end
3232

@@ -35,20 +35,19 @@
3535

3636
it 'marks params with empty values' do
3737
expect { |b| iterator.each(&b) }.to yield_successive_args(
38-
[params[0], :first, true, false], [params[0], :second, true, false],
39-
[params[1], :first, true, false], [params[1], :second, true, false],
40-
[params[2], :first, false, false], [params[2], :second, false, false]
38+
[params[0], :first, true], [params[0], :second, true],
39+
[params[1], :first, true], [params[1], :second, true],
40+
[params[2], :first, false], [params[2], :second, false]
4141
)
4242
end
4343
end
4444

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

48-
it 'marks params with skipped values' do
48+
it 'does not yield skipped values' do
4949
expect { |b| iterator.each(&b) }.to yield_successive_args(
50-
[params[0], :first, false, true], [params[0], :second, false, true],
51-
[params[1], :first, false, false], [params[1], :second, false, false]
50+
[params[1], :first, false], [params[1], :second, false]
5251
)
5352
end
5453
end

0 commit comments

Comments
 (0)