Skip to content

Commit 0a6c804

Browse files
committed
Fix allow_blank validator such that validation will not fail when a value of false is sent in a boolean param and allow_blank is set to false.
1 parent cf11a5e commit 0a6c804

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* [#942](https://github.com/intridea/grape/pull/942): Fixed forced presence for optional params when based on a reused entity that was also required in another context - [@croeck](https://github.com/croeck).
2121
* [#1001](https://github.com/intridea/grape/pull/1001): Fixed calling endpoint with specified format with format in its path - [@hodak](https://github.com/hodak).
2222
* [#1005](https://github.com/intridea/grape/pull/1005): Fixed the Grape::Middleware::Globals - [@urkle](https://github.com/urkle).
23-
23+
* [#1012](https://github.com/intridea/grape/pull/1012): Fixed `allow_blank: false` with a Boolean value of `false` - [@mfunaro](https://github.com/mfunaro).
2424
* Your contribution here.
2525

2626
0.11.0 (2/23/2015)

lib/grape/validations/validators/allow_blank.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def validate_param!(attr_name, params)
2121

2222
return unless should_validate
2323

24-
unless value.present?
24+
unless value == false || value.present?
2525
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message_key: :blank
2626
end
2727
end

spec/grape/validations/validators/allow_blank_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class API < Grape::API
6666
end
6767
get '/allow_boolean_blank'
6868

69+
params do
70+
requires :val, type: Boolean, allow_blank: false
71+
end
72+
get '/disallow_boolean_blank'
73+
6974
params do
7075
optional :user, type: Hash do
7176
requires :name, allow_blank: false
@@ -201,6 +206,11 @@ def app
201206
get '/allow_boolean_blank', val: ''
202207
expect(last_response.status).to eq(200)
203208
end
209+
210+
it 'accepts false when boolean allow_blank' do
211+
get '/disallow_boolean_blank', val: false
212+
expect(last_response.status).to eq(200)
213+
end
204214
end
205215

206216
context 'in an optional group' do

0 commit comments

Comments
 (0)