Skip to content

Commit

Permalink
Support allow nil in length validator
Browse files Browse the repository at this point in the history
  • Loading branch information
OuYangJinTing committed Jul 9, 2024
1 parent da9815d commit 63534f3
Show file tree
Hide file tree
Showing 5 changed files with 41 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): Support allow `nil` in `length` validator - [@OuYangJinTing](https://github.com/OuYangJinTing).
* Your contribution here.

#### Fixes
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1714,12 +1714,13 @@ end

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.
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 need to allow `nil` value, must to pass the `allow_nil: true` option.

```ruby
params do
requires :str, type: String, length: { min: 3 }
requires :list, type: [Integer], length: { min: 3, max: 5 }
requires :list, type: [Integer], length: { min: 3, max: 5, allow_nil: true }
requires :hash, type: Hash, length: { max: 5 }
end
```
Expand Down
1 change: 1 addition & 0 deletions lib/grape/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ en:
presence: 'is missing'
regexp: 'is invalid'
blank: 'is empty'
nil: 'is nil'
values: 'does not have a valid value'
except_values: 'has a value not allowed'
same_as: 'is not the same as %{parameter}'
Expand Down
7 changes: 7 additions & 0 deletions lib/grape/validations/validators/length_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class LengthValidator < Base
def initialize(attrs, options, required, scope, **opts)
@min = options[:min]
@max = options[:max]
@allow_nil = options[:allow_nil]

super

Expand All @@ -18,6 +19,12 @@ def initialize(attrs, options, required, scope, **opts)
def validate_param!(attr_name, params)
param = params[attr_name]

if param.nil?
return if @allow_nil

raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:nil))
end

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
29 changes: 29 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: 0, allow_nil: true }
end
post 'allow_nil_param' do
end

params do
requires :list, type: [Integer], length: { min: 0, allow_nil: false }
end
post 'disallow_nil_param' do
end

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

describe 'when a nil value is passed' do
context '/allow_nil_param' do
it do
post '/allow_nil_param', list: nil
expect(last_response.status).to eq(201)
end
end

context '/disallow_nil_param' do
it do
post '/disallow_nil_param', list: nil
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('list is nil')
end
end
end

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

0 comments on commit 63534f3

Please sign in to comment.