Skip to content

Commit 5bf5a70

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

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#### Features
44

55
* [#2475](https://github.com/ruby-grape/grape/pull/2475): Remove Grape::Util::Registrable - [@ericproulx](https://github.com/ericproulx).
6+
* [#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).
67
* Your contribution here.
78

89
#### Fixes

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)