Skip to content

Commit

Permalink
make regexp allowing nil value
Browse files Browse the repository at this point in the history
  • Loading branch information
calfzhou committed Mar 16, 2015
1 parent 489cdc9 commit 083891d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ end
#### `regexp`

Parameters can be restricted to match a specific regular expression with the `:regexp` option. If the value
is nil or does not match the regular expression an error will be returned. Note that this is true for both `requires`
does not match the regular expression an error will be returned. Note that this is true for both `requires`
and `optional` parameters.

```ruby
Expand All @@ -829,6 +829,14 @@ params do
end
```

`regexp` will not fail if the parameter was sent without value. To ensure that the parameter contain
a value, use `allow_blank` validator.

```ruby
params do
requires :email, allow_blank: false, regexp: /.+@.+/
end
```

#### `mutually_exclusive`

Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/validators/regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Validations
class RegexpValidator < Base
def validate_param!(attr_name, params)
if params.key?(attr_name) &&
(params[attr_name].nil? || !(params[attr_name].to_s =~ @option))
!params[attr_name].nil? && !(params[attr_name].to_s =~ @option)
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message_key: :regexp
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/grape/validations/validators/presence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def app
context 'with a required non-empty string' do
before do
subject.params do
requires :email, type: String, regexp: /^\S+$/
requires :email, type: String, allow_blank: false, regexp: /^\S+$/
end
subject.get do
'Hello'
Expand All @@ -64,12 +64,12 @@ def app
it 'requires when missing' do
get '/'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"email is missing, email is invalid"}')
expect(last_response.body).to eq('{"error":"email is missing, email is empty"}')
end
it 'requires when empty' do
get '/', email: ''
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('{"error":"email is invalid"}')
expect(last_response.body).to eq('{"error":"email is empty, email is invalid"}')
end
it 'valid when set' do
get '/', email: '[email protected]'
Expand Down
9 changes: 7 additions & 2 deletions spec/grape/validations/validators/regexp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ def app
expect(last_response.status).to eq(400)
end

it 'refuses nil' do
get '/', name: nil
it 'refuses empty' do
get '/', name: ''
expect(last_response.status).to eq(400)
end
end

it 'accepts nil' do
get '/', name: nil
expect(last_response.status).to eq(200)
end

it 'accepts valid input' do
get '/', name: 'bob'
expect(last_response.status).to eq(200)
Expand Down

0 comments on commit 083891d

Please sign in to comment.