Skip to content

Commit 081d09b

Browse files
authored
Merge pull request #1567 from jlfaber/fix_values_validator
Fix ValuesValidator and simplify code
2 parents 9d3dcf7 + 0e6f94e commit 081d09b

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* [#1562](https://github.com/ruby-grape/grape/pull/1562): Fix rainbow gem installation failure above ruby 2.3.3 on travis-ci - [@brucehsu](https://github.com/brucehsu).
1212
* [#1561](https://github.com/ruby-grape/grape/pull/1561): Fix performance issue introduced by duplicated calls in StackableValue#[] - [@brucehsu](https://github.com/brucehsu).
1313
* [#1564](https://github.com/ruby-grape/grape/pull/1564): Fix declared params bug with nested namespaces - [@bmarini](https://github.com/bmarini).
14+
* [#1567](https://github.com/ruby-grape/grape/pull/1567): Fix values validator when value is empty array and apply except to input array - [@jlfaber](https://github.com/jlfaber).
1415
* Your contribution here.
1516

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

lib/grape/validations/validators/values.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ module Grape
22
module Validations
33
class ValuesValidator < Base
44
def initialize(attrs, options, required, scope, opts = {})
5-
@excepts = (options_key?(:except, options) ? options[:except] : [])
6-
@values = (options_key?(:value, options) ? options[:value] : [])
7-
8-
@values = options if @excepts == [] && @values == []
5+
if options.is_a?(Hash)
6+
@excepts = options[:except]
7+
@values = options[:value]
8+
else
9+
@values = options
10+
end
911
super
1012
end
1113

@@ -17,12 +19,11 @@ def validate_param!(attr_name, params)
1719
excepts = @excepts.is_a?(Proc) ? @excepts.call : @excepts
1820
param_array = params[attr_name].nil? ? [nil] : Array.wrap(params[attr_name])
1921

20-
if !param_array.empty? && param_array.all? { |param| excepts.include?(param) }
21-
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: except_message
22-
end
22+
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: except_message \
23+
if !excepts.nil? && param_array.any? { |param| excepts.include?(param) }
2324

24-
return if (values.is_a?(Array) && values.empty?) || param_array.all? { |param| values.include?(param) }
25-
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:values)
25+
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:values) \
26+
if !values.nil? && !param_array.all? { |param| values.include?(param) }
2627
end
2728

2829
private

spec/grape/validations/validators/values_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ class API < Grape::API
7676
{ type: params[:type] }
7777
end
7878

79+
params do
80+
requires :type, values: []
81+
end
82+
get '/empty'
83+
7984
params do
8085
optional :type, values: ValuesModel.values, default: 'valid-type2'
8186
end
@@ -90,6 +95,11 @@ class API < Grape::API
9095
{ type: params[:type] }
9196
end
9297

98+
params do
99+
requires :type, values: -> { [] }
100+
end
101+
get '/empty_lambda'
102+
93103
params do
94104
optional :type, values: ValuesModel.values, default: -> { ValuesModel.values.sample }
95105
end
@@ -236,6 +246,12 @@ def app
236246
expect(last_response.body).to eq({ error: 'type does not have a valid value' }.to_json)
237247
end
238248

249+
it 'rejects all values if values is an empty array' do
250+
get('/empty', type: 'invalid-type')
251+
expect(last_response.status).to eq 400
252+
expect(last_response.body).to eq({ error: 'type does not have a valid value' }.to_json)
253+
end
254+
239255
context 'nil value for a parameter' do
240256
it 'does not allow for root params scope' do
241257
get('/', type: nil)
@@ -288,6 +304,12 @@ def app
288304
expect(last_response.body).to eq({ error: 'type does not have a valid value' }.to_json)
289305
end
290306

307+
it 'validates against an empty array in a proc' do
308+
get('/empty_lambda', type: 'any')
309+
expect(last_response.status).to eq 400
310+
expect(last_response.body).to eq({ error: 'type does not have a valid value' }.to_json)
311+
end
312+
291313
it 'validates default value from proc' do
292314
get('/default_lambda')
293315
expect(last_response.status).to eq 200
@@ -428,6 +450,12 @@ def app
428450
expect(last_response.status).to eq 400
429451
expect(last_response.body).to eq({ error: 'type has a value not allowed' }.to_json)
430452
end
453+
454+
it 'rejects an array of values if any of them matches except' do
455+
get '/except/exclusive', type: %w(valid1 valid2 invalid-type1 valid4)
456+
expect(last_response.status).to eq 400
457+
expect(last_response.body).to eq({ error: 'type has a value not allowed' }.to_json)
458+
end
431459
end
432460

433461
context 'exclusive excepts with lambda' do

0 commit comments

Comments
 (0)