Skip to content

Commit

Permalink
Merge pull request #1850 from glaucocustodio/same-as-validator
Browse files Browse the repository at this point in the history
Add same_as validator
  • Loading branch information
dblock authored Dec 17, 2018
2 parents b368380 + 2efbd01 commit d2edd88
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Features

* Your contribution here.
* [#1850](https://github.com/ruby-grape/grape/pull/1850): Adds `same_as` validator - [@glaucocustodio](https://github.com/glaucocustodio).
* [#1833](https://github.com/ruby-grape/grape/pull/1833): Allows to set the `ParamBuilder` globally - [@myxoh](https://github.com/myxoh).
* [#1844](https://github.com/ruby-grape/grape/pull/1844): Fix: enforce `:tempfile` to be a `Tempfile` object in `File` validator - [@Nyangawa](https://github.com/Nyangawa).

Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
- [allow_blank](#allow_blank)
- [values](#values)
- [except_values](#except_values)
- [same_as](#same_as)
- [regexp](#regexp)
- [mutually_exclusive](#mutually_exclusive)
- [exactly_one_of](#exactly_one_of)
Expand All @@ -62,6 +63,7 @@
- [I18n](#i18n)
- [Custom Validation messages](#custom-validation-messages)
- [presence, allow_blank, values, regexp](#presence-allow_blank-values-regexp)
- [same_as](#same_as-1)
- [all_or_none_of](#all_or_none_of-1)
- [mutually_exclusive](#mutually_exclusive-1)
- [exactly_one_of](#exactly_one_of-1)
Expand Down Expand Up @@ -1351,6 +1353,17 @@ params do
end
```

#### `same_as`

A `same_as` option can be given to ensure that values of parameters match.

```ruby
params do
requires :password
requires :password_confirmation, same_as: :password
end
```

#### `regexp`

Parameters can be restricted to match a specific regular expression with the `:regexp` option. If the value
Expand Down Expand Up @@ -1663,6 +1676,15 @@ params do
end
```

#### `same_as`

```ruby
params do
requires :password
requires :password_confirmation, same_as: { value: :password, message: 'not match' }
end
```

#### `all_or_none_of`

```ruby
Expand Down
1 change: 1 addition & 0 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ module ServeFile
require 'grape/validations/validators/mutual_exclusion'
require 'grape/validations/validators/presence'
require 'grape/validations/validators/regexp'
require 'grape/validations/validators/same_as'
require 'grape/validations/validators/values'
require 'grape/validations/validators/except_values'
require 'grape/validations/params_scope'
Expand Down
1 change: 1 addition & 0 deletions lib/grape/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ en:
blank: 'is empty'
values: 'does not have a valid value'
except_values: 'has a value not allowed'
same_as: 'is not the same as %{parameter}'
missing_vendor_option:
problem: 'missing :vendor option.'
summary: 'when version using header, you must specify :vendor option. '
Expand Down
23 changes: 23 additions & 0 deletions lib/grape/validations/validators/same_as.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Grape
module Validations
class SameAsValidator < Base
def validate_param!(attr_name, params)
confirmation = options_key?(:value) ? @option[:value] : @option
return if params[attr_name] == params[confirmation]
raise Grape::Exceptions::Validation,
params: [@scope.full_name(attr_name)],
message: build_message
end

private

def build_message
if options_key?(:message)
@option[:message]
else
format I18n.t(:same_as, scope: 'grape.errors.messages'), parameter: @option
end
end
end
end
end
63 changes: 63 additions & 0 deletions spec/grape/validations/validators/same_as_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'spec_helper'

describe Grape::Validations::SameAsValidator do
module ValidationsSpec
module SameAsValidatorSpec
class API < Grape::API
params do
requires :password
requires :password_confirmation, same_as: :password
end
post do
end

params do
requires :password
requires :password_confirmation, same_as: { value: :password, message: 'not match' }
end
post '/custom-message' do
end
end
end
end

def app
ValidationsSpec::SameAsValidatorSpec::API
end

describe '/' do
context 'is the same' do
it do
post '/', password: '987654', password_confirmation: '987654'
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('')
end
end

context 'is not the same' do
it do
post '/', password: '123456', password_confirmation: 'whatever'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('password_confirmation is not the same as password')
end
end
end

describe '/custom-message' do
context 'is the same' do
it do
post '/custom-message', password: '987654', password_confirmation: '987654'
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('')
end
end

context 'is not the same' do
it do
post '/custom-message', password: '123456', password_confirmation: 'whatever'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('password_confirmation not match')
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
config.include Rack::Test::Methods
config.include Spec::Support::Helpers
config.raise_errors_for_deprecations!
config.filter_run_when_matching :focus

config.before(:each) { Grape::Util::InheritableSetting.reset_global! }
end
Expand Down

0 comments on commit d2edd88

Please sign in to comment.