Skip to content

Commit ee5298f

Browse files
committed
Allow nil in length validator
1 parent da9815d commit ee5298f

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### Features
44

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

78
#### Fixes

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,8 +1713,8 @@ end
17131713
#### `length`
17141714

17151715
Parameters with types that support `#length` method can be restricted to have a specific length with the `:length` option.
1716-
1717-
The validator accepts `:min` or `:max` or both options to validate that the value of the parameter is within the given limits.
1716+
In addition, if the received parameter value is `nil`, the length validation will not be triggered.
1717+
If you want to reject `nil`, you can use the `allow_blank: false` option (This will also reject empty strings).
17181718

17191719
```ruby
17201720
params do

lib/grape/validations/validators/length_validator.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def initialize(attrs, options, required, scope, **opts)
1818
def validate_param!(attr_name, params)
1919
param = params[attr_name]
2020

21+
return if param.nil?
22+
2123
raise ArgumentError, "parameter #{param} does not support #length" unless param.respond_to?(:length)
2224

2325
return unless (!@min.nil? && param.length < @min) || (!@max.nil? && param.length > @max)

spec/grape/validations/validators/length_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@
8181
post 'zero_max' do
8282
end
8383

84+
params do
85+
requires :list, type: [Integer], length: { min: 2 }
86+
end
87+
post 'nil_param' do
88+
end
89+
90+
params do
91+
requires :list, type: [Integer], length: { min: 2 }, allow_blank: false
92+
end
93+
post 'length_with_disallow_blank' do
94+
end
95+
8496
params do
8597
requires :list, type: [Integer], length: { min: 2, message: 'not match' }
8698
end
@@ -187,6 +199,42 @@
187199
end
188200
end
189201

202+
describe '/nil_param' do
203+
context 'does not raise an error' do
204+
it do
205+
expect do
206+
post '/nil_param', list: nil
207+
end.not_to raise_error
208+
end
209+
end
210+
end
211+
212+
describe '/length_with_disallow_blank' do
213+
context 'when length is within limits' do
214+
it do
215+
post '/length_with_disallow_blank', list: [1, 2]
216+
expect(last_response.status).to eq(201)
217+
expect(last_response.body).to eq('')
218+
end
219+
end
220+
221+
context 'when a nil value is passed' do
222+
it do
223+
post '/length_with_disallow_blank', list: nil
224+
expect(last_response.status).to eq(400)
225+
expect(last_response.body).to eq('list is empty')
226+
end
227+
end
228+
229+
context 'when a empty string is passed' do
230+
it do
231+
post '/length_with_disallow_blank', list: ''
232+
expect(last_response.status).to eq(400)
233+
expect(last_response.body).to eq('list is empty')
234+
end
235+
end
236+
end
237+
190238
describe '/type_is_not_array' do
191239
context 'raises an error' do
192240
it do

0 commit comments

Comments
 (0)