Skip to content

Commit 16057ad

Browse files
authored
Merge pull request #1963 from ruby-grape/bugfix/1577
The values validator must properly work with booleans
2 parents 9feefdf + bcf9e81 commit 16057ad

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

CHANGELOG.md

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

1717
#### Fixes
1818

19+
* [#1963](https://github.com/ruby-grape/grape/pull/1963): The values validator must properly work with booleans - [@dnesteryuk](https://github.com/dnesteryuk).
1920
* [#1950](https://github.com/ruby-grape/grape/pull/1950): Consider the allow_blank option in the values validator - [@dnesteryuk](https://github.com/dnesteryuk).
2021
* [#1947](https://github.com/ruby-grape/grape/pull/1947): Careful check for empty params - [@dnesteryuk](https://github.com/dnesteryuk).
2122
* [#1931](https://github.com/ruby-grape/grape/pull/1946): Fixes issue when using namespaces in `Grape::API::Instance` mounted directly - [@myxoh](https://github.com/myxoh).

lib/grape/validations/validators/values.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ def validate_param!(attr_name, params)
2929

3030
val = params[attr_name]
3131

32-
return unless val || required_for_root_scope?
32+
return if val.nil? && !required_for_root_scope?
3333

3434
# don't forget that +false.blank?+ is true
3535
return if val != false && val.blank? && @allow_blank
3636

3737
param_array = val.nil? ? [nil] : Array.wrap(val)
3838

39-
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: except_message) \
39+
raise validation_exception(attr_name, except_message) \
4040
unless check_excepts(param_array)
4141

42-
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:values)) \
42+
raise validation_exception(attr_name, message(:values)) \
4343
unless check_values(param_array, attr_name)
4444

45-
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:values)) \
45+
raise validation_exception(attr_name, message(:values)) \
4646
if @proc && !param_array.all? { |param| @proc.call(param) }
4747
end
4848

@@ -74,6 +74,10 @@ def except_message
7474
def required_for_root_scope?
7575
@required && @scope.root?
7676
end
77+
78+
def validation_exception(attr_name, message)
79+
Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
80+
end
7781
end
7882
end
7983
end

spec/grape/validations/validators/values_spec.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,21 @@ def app
438438
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
439439
end
440440

441-
it 'allows values to be true or false when setting the type to boolean' do
442-
get('/values/optional_boolean', type: true)
443-
expect(last_response.status).to eq 200
444-
expect(last_response.body).to eq({ type: true }.to_json)
441+
context 'boolean values' do
442+
it 'allows a value from the list' do
443+
get('/values/optional_boolean', type: true)
444+
445+
expect(last_response.status).to eq 200
446+
expect(last_response.body).to eq({ type: true }.to_json)
447+
end
448+
449+
it 'rejects a value which is not in the list' do
450+
get('/values/optional_boolean', type: false)
451+
452+
expect(last_response.body).to eq({ error: 'type does not have a valid value' }.to_json)
453+
end
445454
end
455+
446456
it 'allows values to be a kind of the coerced type not just an instance of it' do
447457
get('/values/coercion', type: 10)
448458
expect(last_response.status).to eq 200

0 commit comments

Comments
 (0)