Skip to content

Commit f3e82d9

Browse files
calfzhoudblock
authored andcommitted
Regexp validator now supports allow_blank, nil value behavior changed.
1 parent 489cdc9 commit f3e82d9

File tree

6 files changed

+38
-12
lines changed

6 files changed

+38
-12
lines changed

CHANGELOG.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
0.11.1 (Next)
1+
0.12.0 (Next)
22
=============
33

44
#### Fixes
55

66
* [#936](https://github.com/intridea/grape/pull/936): Fixed default params processing for optional groups - [@dm1try](https://github.com/dm1try).
77
* [#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).
8-
* [#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).
9-
* [#952](https://github.com/intridea/grape/pull/952): Status method now raises error when called with invalid status code. - [@dabrorius](https://github.com/dabrorius).
10-
8+
* [#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).
9+
* [#952](https://github.com/intridea/grape/pull/952): Status method now raises error when called with invalid status code - [@dabrorius](https://github.com/dabrorius).
10+
* [#957](https://github.com/intridea/grape/pull/957): Regexp validator now supports `allow_blank`, `nil` value behavior changed - [@calfzhou](https://giihub.com/calfzhou).
1111
* Your contribution here.
1212

1313
0.11.0 (2/23/2015)

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ end
820820
#### `regexp`
821821

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

826826
```ruby
@@ -829,6 +829,13 @@ params do
829829
end
830830
```
831831

832+
The validator will pass if the parameter was sent without value. To ensure that the parameter contains a value, use `allow_blank: false`.
833+
834+
```ruby
835+
params do
836+
requires :email, allow_blank: false, regexp: /.+@.+/
837+
end
838+
```
832839

833840
#### `mutually_exclusive`
834841

UPGRADING.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
Upgrading Grape
22
===============
33

4+
### Upgrading to >= 0.12.0
5+
6+
#### Changes to regexp validator
7+
8+
Parameters with `nil` value will now pass `regexp` validation. To disallow `nil` value for an endpoint, add `allow_blank: false`.
9+
10+
```ruby
11+
params do
12+
requires :email, allow_blank: false, regexp: /.+@.+/
13+
end
14+
```
15+
16+
See [#957](https://github.com/intridea/grape/pull/957) for more information.
17+
418
### Upgrading to >= 0.11.0
519

6-
#### Added Rack 1.6.0 Support
20+
#### Added Rack 1.6.0 support
721

822
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.
923

lib/grape/validations/validators/regexp.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Validations
33
class RegexpValidator < Base
44
def validate_param!(attr_name, params)
55
if params.key?(attr_name) &&
6-
(params[attr_name].nil? || !(params[attr_name].to_s =~ @option))
6+
!params[attr_name].nil? && !(params[attr_name].to_s =~ @option)
77
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message_key: :regexp
88
end
99
end

spec/grape/validations/validators/presence_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def app
5555
context 'with a required non-empty string' do
5656
before do
5757
subject.params do
58-
requires :email, type: String, regexp: /^\S+$/
58+
requires :email, type: String, allow_blank: false, regexp: /^\S+$/
5959
end
6060
subject.get do
6161
'Hello'
@@ -64,12 +64,12 @@ def app
6464
it 'requires when missing' do
6565
get '/'
6666
expect(last_response.status).to eq(400)
67-
expect(last_response.body).to eq('{"error":"email is missing, email is invalid"}')
67+
expect(last_response.body).to eq('{"error":"email is missing, email is empty"}')
6868
end
6969
it 'requires when empty' do
7070
get '/', email: ''
7171
expect(last_response.status).to eq(400)
72-
expect(last_response.body).to eq('{"error":"email is invalid"}')
72+
expect(last_response.body).to eq('{"error":"email is empty, email is invalid"}')
7373
end
7474
it 'valid when set' do
7575
get '/', email: 'bob@example.com'

spec/grape/validations/validators/regexp_spec.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ def app
2525
expect(last_response.status).to eq(400)
2626
end
2727

28-
it 'refuses nil' do
29-
get '/', name: nil
28+
it 'refuses empty' do
29+
get '/', name: ''
3030
expect(last_response.status).to eq(400)
3131
end
3232
end
3333

34+
it 'accepts nil' do
35+
get '/', name: nil
36+
expect(last_response.status).to eq(200)
37+
end
38+
3439
it 'accepts valid input' do
3540
get '/', name: 'bob'
3641
expect(last_response.status).to eq(200)

0 commit comments

Comments
 (0)