Skip to content

Commit

Permalink
The length validator only takes effect for parameters with types th…
Browse files Browse the repository at this point in the history
…at support `#length` method
  • Loading branch information
OuYangJinTing committed Jul 25, 2024
1 parent 2b8567a commit 5bf5a70
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/validators/length_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions spec/grape/validations/validators/length_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5bf5a70

Please sign in to comment.