From 8dacd2dad4142485c15628d5c6b72d81eb15c964 Mon Sep 17 00:00:00 2001 From: Ji Zhou Date: Mon, 16 Mar 2015 11:40:23 +0800 Subject: [PATCH 1/2] make `regexp` allowing nil value update changelog --- CHANGELOG.md | 1 + README.md | 10 +++++++++- lib/grape/validations/validators/regexp.rb | 2 +- spec/grape/validations/validators/presence_spec.rb | 6 +++--- spec/grape/validations/validators/regexp_spec.rb | 9 +++++++-- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec2f7040a..a2b712e86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index e22ecb284..2a9d8b422 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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` diff --git a/lib/grape/validations/validators/regexp.rb b/lib/grape/validations/validators/regexp.rb index 0f4d3f469..5e64ddd70 100644 --- a/lib/grape/validations/validators/regexp.rb +++ b/lib/grape/validations/validators/regexp.rb @@ -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 diff --git a/spec/grape/validations/validators/presence_spec.rb b/spec/grape/validations/validators/presence_spec.rb index 0bfb9e231..980bec1d8 100644 --- a/spec/grape/validations/validators/presence_spec.rb +++ b/spec/grape/validations/validators/presence_spec.rb @@ -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' @@ -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' diff --git a/spec/grape/validations/validators/regexp_spec.rb b/spec/grape/validations/validators/regexp_spec.rb index c02489cb5..5c5b35303 100644 --- a/spec/grape/validations/validators/regexp_spec.rb +++ b/spec/grape/validations/validators/regexp_spec.rb @@ -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) From a5dee6349196ac1877ba63321125ecfa1e1c2139 Mon Sep 17 00:00:00 2001 From: Ji Zhou Date: Tue, 17 Mar 2015 11:03:55 +0800 Subject: [PATCH 2/2] move next release version to 0.12.0 and add upgrading notes --- CHANGELOG.md | 2 +- UPGRADING.md | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2b712e86..e578ac349 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -0.11.1 (Next) +0.12.0 (Next) ============= #### Fixes diff --git a/UPGRADING.md b/UPGRADING.md index 295fa3a6d..6d8904562 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -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