Skip to content

Commit

Permalink
make regexp allowing nil value
Browse files Browse the repository at this point in the history
update changelog
  • Loading branch information
calfzhou committed Mar 16, 2015
1 parent 489cdc9 commit b365f5c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [#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).
* [#950](https://github.com/intridea/grape/pull/950): Status method can now accept one of Rack::Utils status code symbols (:ok, :found, :bad_request, etc.). - [@dabrorius](https://github.com/dabrorius).
* [#952](https://github.com/intridea/grape/pull/952): Status method now raises error when called with invalid status code. - [@dabrorius](https://github.com/dabrorius).
* [#957](https://github.com/intridea/grape/pull/957): Params with `nil` value are now valid to `regexp` validator. Use additional `allow_blank: false` if `nil` value is not valid to the endpoint. -[@calfzhou](https://giihub.com/calfzhou).

* Your contribution here.

Expand Down
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 b365f5c

Please sign in to comment.