Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add same_as validator #1850

Merged
merged 1 commit into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

@glaucocustodio glaucocustodio Dec 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is out of scope of this PR, but I find it to be very useful. Can we keep?


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