Skip to content

Commit

Permalink
Regexp validator now supports allow_blank, nil value behavior changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
calfzhou authored and dblock committed Mar 18, 2015
1 parent 489cdc9 commit f3e82d9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 12 deletions.
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
0.11.1 (Next)
0.12.0 (Next)
=============

#### Fixes

* [#936](https://github.com/intridea/grape/pull/936): Fixed default params processing for optional groups - [@dm1try](https://github.com/dm1try).
* [#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).

* [#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): Regexp validator now supports `allow_blank`, `nil` value behavior changed - [@calfzhou](https://giihub.com/calfzhou).
* Your contribution here.

0.11.0 (2/23/2015)
Expand Down
9 changes: 8 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,13 @@ params do
end
```

The validator will pass if the parameter was sent without value. To ensure that the parameter contains a value, use `allow_blank: false`.

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

#### `mutually_exclusive`

Expand Down
16 changes: 15 additions & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
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, add `allow_blank: false`.

```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
#### Added Rack 1.6.0 support

Grape now supports, but doesn't require Rack 1.6.0. If you encounter an issue with parsing requests larger than 128KB, explictly require Rack 1.6.0 in your Gemfile.

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 f3e82d9

Please sign in to comment.