Skip to content

Commit 99c8468

Browse files
committed
Merge pull request #614 from dm1try/refuse_params_with_nil_by_regex_validator
regex validator refuses params with nil value
2 parents 9d78297 + e2150b5 commit 99c8468

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

CHANGELOG.md

+4
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

+2-1
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

+10-3
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)