diff --git a/CHANGELOG.md b/CHANGELOG.md index e7380dded..1c4b3632d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/grape/validations/attributes_iterator.rb b/lib/grape/validations/attributes_iterator.rb index c3f6ed53c..59b3790c3 100644 --- a/lib/grape/validations/attributes_iterator.rb +++ b/lib/grape/validations/attributes_iterator.rb @@ -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 @@ -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 @@ -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 diff --git a/lib/grape/validations/multiple_attributes_iterator.rb b/lib/grape/validations/multiple_attributes_iterator.rb index e5621bcaa..e8224fc6a 100644 --- a/lib/grape/validations/multiple_attributes_iterator.rb +++ b/lib/grape/validations/multiple_attributes_iterator.rb @@ -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 diff --git a/lib/grape/validations/single_attribute_iterator.rb b/lib/grape/validations/single_attribute_iterator.rb index 218f4037b..4845a5631 100644 --- a/lib/grape/validations/single_attribute_iterator.rb +++ b/lib/grape/validations/single_attribute_iterator.rb @@ -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 diff --git a/lib/grape/validations/validators/base.rb b/lib/grape/validations/validators/base.rb index 4fd64acef..58e3b65bd 100644 --- a/lib/grape/validations/validators/base.rb +++ b/lib/grape/validations/validators/base.rb @@ -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 = [] diff --git a/lib/grape/validations/validators/default_validator.rb b/lib/grape/validations/validators/default_validator.rb index eba8c7730..157aa988c 100644 --- a/lib/grape/validations/validators/default_validator.rb +++ b/lib/grape/validations/validators/default_validator.rb @@ -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) diff --git a/lib/grape/validations/validators/multiple_params_base.rb b/lib/grape/validations/validators/multiple_params_base.rb index 29df27720..edde68e6c 100644 --- a/lib/grape/validations/validators/multiple_params_base.rb +++ b/lib/grape/validations/validators/multiple_params_base.rb @@ -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| @@ -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 diff --git a/spec/grape/validations/single_attribute_iterator_spec.rb b/spec/grape/validations/single_attribute_iterator_spec.rb index 2cde615e3..a0e5a708c 100644 --- a/spec/grape/validations/single_attribute_iterator_spec.rb +++ b/spec/grape/validations/single_attribute_iterator_spec.rb @@ -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