Skip to content

Commit e2150b5

Browse files
author
dm1try
committed
regex validator refuses params with nil value
1 parent 9d78297 commit e2150b5

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ Next Release
33

44
* Your contribution here.
55

6+
#### Fixes
7+
8+
* [#614](https://github.com/intridea/grape/pull/614): Params with `nil` value are now refused by `RegexpValidator` - [@dm1try](https://github.com/dm1try).
9+
610
0.7.0 (4/2/2013)
711
=================
812

lib/grape/validations/regexp.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ module Grape
22
module Validations
33
class RegexpValidator < SingleOptionValidator
44
def validate_param!(attr_name, params)
5-
if params[attr_name] && !(params[attr_name].to_s =~ @option)
5+
if params.has_key?(attr_name) &&
6+
(params[attr_name].nil? || !(params[attr_name].to_s =~ @option))
67
raise Grape::Exceptions::Validation, param: @scope.full_name(attr_name), message_key: :regexp
78
end
89
end

spec/grape/validations/regexp_spec.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ def app
2020
ValidationsSpec::RegexpValidatorSpec::API
2121
end
2222

23-
it 'refuses invalid input' do
24-
get '/', name: "invalid name"
25-
last_response.status.should == 400
23+
context 'invalid input' do
24+
it 'refuses inapppopriate' do
25+
get '/', name: "invalid name"
26+
last_response.status.should == 400
27+
end
28+
29+
it 'refuses nil' do
30+
get '/', name: nil
31+
last_response.status.should == 400
32+
end
2633
end
2734

2835
it 'accepts valid input' do

0 commit comments

Comments
 (0)