Skip to content

Commit

Permalink
Allow nil in length validator
Browse files Browse the repository at this point in the history
  • Loading branch information
OuYangJinTing committed Jul 15, 2024
1 parent da9815d commit ec83cc0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### Features

* [#2464](https://github.com/ruby-grape/grape/pull/2464): Allow `nil` in `length` validator - [@OuYangJinTing](https://github.com/OuYangJinTing).
* Your contribution here.

#### Fixes
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1713,8 +1713,8 @@ end
#### `length`

Parameters with types that support `#length` method can be restricted to have a specific length with the `:length` option.

The validator accepts `:min` or `:max` or both options to validate that the value of the parameter is within the given limits.
In addition, if the received parameter value is `nil`, the length validation will not be triggered.
If you want to reject `nil`, you can use the `allow_blank: false` option (This will also reject empty strings).

```ruby
params do
Expand Down
2 changes: 2 additions & 0 deletions lib/grape/validations/validators/length_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def initialize(attrs, options, required, scope, **opts)
def validate_param!(attr_name, params)
param = params[attr_name]

return if param.nil?

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

return unless (!@min.nil? && param.length < @min) || (!@max.nil? && param.length > @max)
Expand Down
48 changes: 48 additions & 0 deletions spec/grape/validations/validators/length_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@
post 'zero_max' do
end

params do
requires :list, type: [Integer], length: { min: 2 }
end
post 'nil_param' do
end

params do
requires :list, type: [Integer], length: { min: 2 }, allow_blank: false
end
post 'length_with_disallow_blank' do
end

params do
requires :list, type: [Integer], length: { min: 2, message: 'not match' }
end
Expand Down Expand Up @@ -187,6 +199,42 @@
end
end

describe '/nil_param' do
context 'does not raise an error' do
it do
expect do
post '/nil_param', list: nil
end.not_to raise_error
end
end
end

describe '/length_with_disallow_blank' do
context 'when length is within limits' do
it do
post '/length_with_disallow_blank', list: [1, 2]
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('')
end
end

context 'when a nil value is passed' do
it do
post '/length_with_disallow_blank', list: nil
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('list is empty')
end
end

context 'when a empty string is passed' do
it do
post '/length_with_disallow_blank', list: ''
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('list is expected to have length greater than or equal to 2, list is empty')
end
end
end

describe '/type_is_not_array' do
context 'raises an error' do
it do
Expand Down

0 comments on commit ec83cc0

Please sign in to comment.