Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regex validator refuses params with nil value #614

Merged
merged 1 commit into from
Apr 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Next Release

* Your contribution here.

#### Fixes

* [#614](/~https://github.com/intridea/grape/pull/614): Params with `nil` value are now refused by `RegexpValidator` - [@dm1try](/~https://github.com/dm1try).

0.7.0 (4/2/2013)
=================

Expand Down
3 changes: 2 additions & 1 deletion lib/grape/validations/regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module Grape
module Validations
class RegexpValidator < SingleOptionValidator
def validate_param!(attr_name, params)
if params[attr_name] && !(params[attr_name].to_s =~ @option)
if params.has_key?(attr_name) &&
(params[attr_name].nil? || !(params[attr_name].to_s =~ @option))
raise Grape::Exceptions::Validation, param: @scope.full_name(attr_name), message_key: :regexp
end
end
Expand Down
13 changes: 10 additions & 3 deletions spec/grape/validations/regexp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ def app
ValidationsSpec::RegexpValidatorSpec::API
end

it 'refuses invalid input' do
get '/', name: "invalid name"
last_response.status.should == 400
context 'invalid input' do
it 'refuses inapppopriate' do
get '/', name: "invalid name"
last_response.status.should == 400
end

it 'refuses nil' do
get '/', name: nil
last_response.status.should == 400
end
end

it 'accepts valid input' do
Expand Down