Skip to content

Commit 3b91395

Browse files
ghiculescudblock
authored andcommitted
should be able to pass nil to an optional array values param (#1559)
* fix - should be able to pass `nil` to an optional array values param * update changelog * oops
1 parent b0bc5c9 commit 3b91395

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#### Fixes
99

10+
* [#1559](https://github.com/ruby-grape/grape/pull/1559): You can once again pass `nil` to optional attributes with `values` validation set - [@ghiculescu](https://github.com/ghiculescu).
1011
* Your contribution here.
1112

1213
### 0.19.1 (1/9/2017)

lib/grape/validations/validators/values.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def validate_param!(attr_name, params)
1717
excepts = @excepts.is_a?(Proc) ? @excepts.call : @excepts
1818
param_array = params[attr_name].nil? ? [nil] : Array.wrap(params[attr_name])
1919

20-
if param_array.all? { |param| excepts.include?(param) }
20+
if !param_array.empty? && param_array.all? { |param| excepts.include?(param) }
2121
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: except_message
2222
end
2323

spec/grape/validations/validators/values_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ class API < Grape::API
159159
get '/mixed/value/except' do
160160
{ type: params[:type] }
161161
end
162+
163+
params do
164+
optional :optional, type: Array[String], values: %w(a b c)
165+
end
166+
put '/optional_with_array_of_string_values'
162167
end
163168
end
164169
end
@@ -242,6 +247,11 @@ def app
242247
get('/optional_with_required_values')
243248
expect(last_response.status).to eq 200
244249
end
250+
251+
it 'allows for an optional param with a list of values' do
252+
put('/optional_with_array_of_string_values', optional: nil)
253+
expect(last_response.status).to eq 200
254+
end
245255
end
246256

247257
it 'allows a valid default value' do

0 commit comments

Comments
 (0)