Skip to content
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 @@ -7,6 +7,7 @@
* [#2619](https://github.com/ruby-grape/grape/pull/2619): Remove TOC from README.md and danger-toc check - [@alexanderadam](https://github.com/alexanderadam).
* [#2663](https://github.com/ruby-grape/grape/pull/2663): Refactor `ParamsScope` and `Parameters` DSL to use named kwargs - [@ericproulx](https://github.com/ericproulx).
* [#2664](https://github.com/ruby-grape/grape/pull/2664): Drop `test-prof` dependency - [@ericproulx](https://github.com/ericproulx).
* [#2665](https://github.com/ruby-grape/grape/pull/2665): Pass `attrs` directly to `AttributesIterator` instead of `validator` - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

#### Fixes
Expand Down
8 changes: 4 additions & 4 deletions lib/grape/validations/attributes_iterator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class AttributesIterator

attr_reader :scope

def initialize(validator, scope, params)
def initialize(attrs, scope, params)
@attrs = attrs
@scope = scope
@attrs = validator.attrs
@original_params = scope.params(params)
@params = Array.wrap(@original_params)
end
Expand Down Expand Up @@ -41,7 +41,7 @@ def do_each(params_to_process, parent_indices = [], &block)
store_indices(target, index, parent_indices) if target
end

yield_attributes(resource_params, @attrs, &block)
yield_attributes(resource_params, &block)
end
end

Expand All @@ -61,7 +61,7 @@ def store_indices(target_scope, index, parent_indices)
tracker.store_index(target_scope, index)
end

def yield_attributes(_resource_params, _attrs)
def yield_attributes(_resource_params)
raise NotImplementedError
end

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 @@ -5,7 +5,7 @@ module Validations
class MultipleAttributesIterator < AttributesIterator
private

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

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

attrs.each do |attr_name|
@attrs.each do |attr_name|
yield val, attr_name, empty?(val)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/validators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def validate(request)
# @raise [Grape::Exceptions::Validation] if validation failed
# @return [void]
def validate!(params)
attributes = SingleAttributeIterator.new(self, @scope, params)
attributes = SingleAttributeIterator.new(@attrs, @scope, params)
# we collect errors inside array because
# there may be more than one error per field
array_errors = []
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/validators/default_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def validate_param!(attr_name, params)
end

def validate!(params)
attrs = SingleAttributeIterator.new(self, @scope, params)
attrs = SingleAttributeIterator.new(@attrs, @scope, params)
attrs.each do |resource_params, attr_name|
next unless @scope.meets_dependency?(resource_params, params)

Expand Down
4 changes: 2 additions & 2 deletions lib/grape/validations/validators/multiple_params_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Validations
module Validators
class MultipleParamsBase < Base
def validate!(params)
attributes = MultipleAttributesIterator.new(self, @scope, params)
attributes = MultipleAttributesIterator.new(@attrs, @scope, params)
array_errors = []

attributes.each do |resource_params|
Expand All @@ -26,7 +26,7 @@ def keys_in_common(resource_params)
end

def all_keys
attrs.map { |attr| @scope.full_name(attr) }
@attrs.map { |attr| @scope.full_name(attr) }
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions spec/grape/validations/single_attribute_iterator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

describe Grape::Validations::SingleAttributeIterator do
describe '#each' do
subject(:iterator) { described_class.new(validator, scope, params) }
subject(:iterator) { described_class.new(%i[first second], scope, params) }

let(:scope) { Grape::Validations::ParamsScope.new(api: Class.new(Grape::API)) }
let(:validator) { double(attrs: %i[first second]) }

context 'when params is a hash' do
let(:params) do
Expand Down
Loading