diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f22f7228e..2a7d9890bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ #### Features * [#2475](https://github.com/ruby-grape/grape/pull/2475): Remove Grape::Util::Registrable - [@ericproulx](https://github.com/ericproulx). +* [#2464](https://github.com/ruby-grape/grape/pull/2464): The `length` validator only takes effect for parameters with types that support `#length` method - [@OuYangJinTing](https://github.com/OuYangJinTing). * Your contribution here. #### Fixes diff --git a/lib/grape/validations/validators/length_validator.rb b/lib/grape/validations/validators/length_validator.rb index bcd0c95592..ed266fe842 100644 --- a/lib/grape/validations/validators/length_validator.rb +++ b/lib/grape/validations/validators/length_validator.rb @@ -18,7 +18,7 @@ def initialize(attrs, options, required, scope, **opts) def validate_param!(attr_name, params) param = params[attr_name] - raise ArgumentError, "parameter #{param} does not support #length" unless param.respond_to?(:length) + return unless param.respond_to?(:length) return unless (!@min.nil? && param.length < @min) || (!@max.nil? && param.length > @max) diff --git a/spec/grape/validations/validators/length_spec.rb b/spec/grape/validations/validators/length_spec.rb index 8fa9f84876..9334b7a204 100644 --- a/spec/grape/validations/validators/length_spec.rb +++ b/spec/grape/validations/validators/length_spec.rb @@ -188,11 +188,11 @@ end describe '/type_is_not_array' do - context 'raises an error' do + context 'does not raise an error' do it do expect do post 'type_is_not_array', list: 12 - end.to raise_error(ArgumentError, 'parameter 12 does not support #length') + end.not_to raise_error end end end