Skip to content

Commit b365f5c

Browse files
committed
make regexp allowing nil value
update changelog
1 parent 489cdc9 commit b365f5c

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
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).
88
* [#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).
99
* [#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): 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).
1011

1112
* Your contribution here.
1213

README.md

+9-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,14 @@ params do
829829
end
830830
```
831831

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

833841
#### `mutually_exclusive`
834842

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)