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 26, 2024
1 parent 2b8567a commit ba84149
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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)
Expand Down
8 changes: 8 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -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
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 ba84149

Please sign in to comment.