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 8, 2024
1 parent f1560cd commit 5f6c279
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
### 2.2.0 (Next)

#### 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

* Your contribution here.

### 2.1.2 (2024-06-28)

#### Fixes
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ Grape is a REST-like API framework for Ruby. It's designed to run on Rack or com

## Stable Release

You're reading the documentation for the stable release of Grape, **2.1.2**.
You're reading the documentation for the next release of Grape, which should be 2.2.0.
Please read [UPGRADING](UPGRADING.md) when upgrading from a previous version.
The current stable release is [2.1.2](https://github.com/ruby-grape/grape/blob/v2.1.2/README.md).

## Project Resources

Expand Down Expand Up @@ -1713,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, please 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
2 changes: 1 addition & 1 deletion lib/grape/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module Grape
# The current version of Grape.
VERSION = '2.1.2'
VERSION = '2.2.0'
end
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 }
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 5f6c279

Please sign in to comment.