Skip to content

Commit ba84149

Browse files
committed
The length validator only takes effect for parameters with types that support #length method
1 parent 2b8567a commit ba84149

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* [#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).
1111
* [#2478](https://github.com/ruby-grape/grape/pull/2478): Fix rescue_from with invalid response - [@ericproulx](https://github.com/ericproulx).
1212
* [#2480](https://github.com/ruby-grape/grape/pull/2480): Fix rescue_from ValidationErrors exception - [@numbata](https://github.com/numbata).
13+
* [#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).
1314
* Your contribution here.
1415

1516
### 2.1.3 (2024-07-13)

UPGRADING.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Upgrading Grape
22
===============
33

4+
### Upgrading to >= 2.2.0
5+
6+
### `Length` validator
7+
8+
After Grape 2.2.0, `length` validator will only take effect for parameters with types that support `#length` method, will not throw `ArgumentError` exception.
9+
10+
See [#2464](https://github.com/ruby-grape/grape/pull/2464) for more information.
11+
412
### Upgrading to >= 2.1.0
513

614
#### Optional Builder

lib/grape/validations/validators/length_validator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def initialize(attrs, options, required, scope, **opts)
1818
def validate_param!(attr_name, params)
1919
param = params[attr_name]
2020

21-
raise ArgumentError, "parameter #{param} does not support #length" unless param.respond_to?(:length)
21+
return unless param.respond_to?(:length)
2222

2323
return unless (!@min.nil? && param.length < @min) || (!@max.nil? && param.length > @max)
2424

spec/grape/validations/validators/length_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,11 @@
188188
end
189189

190190
describe '/type_is_not_array' do
191-
context 'raises an error' do
191+
context 'does not raise an error' do
192192
it do
193193
expect do
194194
post 'type_is_not_array', list: 12
195-
end.to raise_error(ArgumentError, 'parameter 12 does not support #length')
195+
end.not_to raise_error
196196
end
197197
end
198198
end

0 commit comments

Comments
 (0)