Skip to content

make regexp allowing nil value #957

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

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
0.11.1 (Next)
0.12.0 (Next)
=============

#### Fixes
Expand All @@ -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
15 changes: 15 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
Upgrading Grape
===============

### Upgrading to >= 0.12.0

#### Changes to `regexp` validator

Parameters with `nil` value will now pass `regexp` validation. To disallow `nil` value for an endpoint,
use `allow_blank` validator for additional check.

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

See [#957](https://github.com/intridea/grape/pull/957) for more information.

### Upgrading to >= 0.11.0

#### Added Rack 1.6.0 Support
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: 'bob@example.com'
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