From 04e69ea120ad2e32348932a82930b7526456766e Mon Sep 17 00:00:00 2001 From: ouyangjinting Date: Sun, 28 Jul 2024 04:39:43 +0800 Subject: [PATCH] The `length` validator only takes effect for parameters with types that support `#length` method (#2464) --- CHANGELOG.md | 1 + UPGRADING.md | 8 ++++++++ lib/grape/validations/validators/length_validator.rb | 2 +- spec/grape/validations/validators/length_spec.rb | 4 ++-- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22ea580d92..48d1b804e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * [#2471](https://github.com/ruby-grape/grape/pull/2471): Fix absence of original_exception and/or backtrace even if passed in error! - [@numbata](https://github.com/numbata). * [#2478](https://github.com/ruby-grape/grape/pull/2478): Fix rescue_from with invalid response - [@ericproulx](https://github.com/ericproulx). * [#2480](https://github.com/ruby-grape/grape/pull/2480): Fix rescue_from ValidationErrors exception - [@numbata](https://github.com/numbata). +* [#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. ### 2.1.3 (2024-07-13) diff --git a/UPGRADING.md b/UPGRADING.md index be2fb564de..cba4774bbe 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -1,6 +1,14 @@ Upgrading Grape =============== +### Upgrading to >= 2.2.0 + +### `Length` validator + +After Grape 2.2.0, `length` validator will only take effect for parameters with types that support `#length` method, will not throw `ArgumentError` exception. + +See [#2464](https://github.com/ruby-grape/grape/pull/2464) for more information. + ### Upgrading to >= 2.1.0 #### Optional Builder 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