Skip to content

Commit d2edd88

Browse files
authored
Merge pull request #1850 from glaucocustodio/same-as-validator
Add same_as validator
2 parents b368380 + 2efbd01 commit d2edd88

7 files changed

Lines changed: 112 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#### Features
44

55
* Your contribution here.
6+
* [#1850](https://github.com/ruby-grape/grape/pull/1850): Adds `same_as` validator - [@glaucocustodio](https://github.com/glaucocustodio).
67
* [#1833](https://github.com/ruby-grape/grape/pull/1833): Allows to set the `ParamBuilder` globally - [@myxoh](https://github.com/myxoh).
78
* [#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).
89

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
- [allow_blank](#allow_blank)
5151
- [values](#values)
5252
- [except_values](#except_values)
53+
- [same_as](#same_as)
5354
- [regexp](#regexp)
5455
- [mutually_exclusive](#mutually_exclusive)
5556
- [exactly_one_of](#exactly_one_of)
@@ -62,6 +63,7 @@
6263
- [I18n](#i18n)
6364
- [Custom Validation messages](#custom-validation-messages)
6465
- [presence, allow_blank, values, regexp](#presence-allow_blank-values-regexp)
66+
- [same_as](#same_as-1)
6567
- [all_or_none_of](#all_or_none_of-1)
6668
- [mutually_exclusive](#mutually_exclusive-1)
6769
- [exactly_one_of](#exactly_one_of-1)
@@ -1351,6 +1353,17 @@ params do
13511353
end
13521354
```
13531355

1356+
#### `same_as`
1357+
1358+
A `same_as` option can be given to ensure that values of parameters match.
1359+
1360+
```ruby
1361+
params do
1362+
requires :password
1363+
requires :password_confirmation, same_as: :password
1364+
end
1365+
```
1366+
13541367
#### `regexp`
13551368

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

1679+
#### `same_as`
1680+
1681+
```ruby
1682+
params do
1683+
requires :password
1684+
requires :password_confirmation, same_as: { value: :password, message: 'not match' }
1685+
end
1686+
```
1687+
16661688
#### `all_or_none_of`
16671689

16681690
```ruby

lib/grape.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ module ServeFile
207207
require 'grape/validations/validators/mutual_exclusion'
208208
require 'grape/validations/validators/presence'
209209
require 'grape/validations/validators/regexp'
210+
require 'grape/validations/validators/same_as'
210211
require 'grape/validations/validators/values'
211212
require 'grape/validations/validators/except_values'
212213
require 'grape/validations/params_scope'

lib/grape/locale/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ en:
99
blank: 'is empty'
1010
values: 'does not have a valid value'
1111
except_values: 'has a value not allowed'
12+
same_as: 'is not the same as %{parameter}'
1213
missing_vendor_option:
1314
problem: 'missing :vendor option.'
1415
summary: 'when version using header, you must specify :vendor option. '
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Grape
2+
module Validations
3+
class SameAsValidator < Base
4+
def validate_param!(attr_name, params)
5+
confirmation = options_key?(:value) ? @option[:value] : @option
6+
return if params[attr_name] == params[confirmation]
7+
raise Grape::Exceptions::Validation,
8+
params: [@scope.full_name(attr_name)],
9+
message: build_message
10+
end
11+
12+
private
13+
14+
def build_message
15+
if options_key?(:message)
16+
@option[:message]
17+
else
18+
format I18n.t(:same_as, scope: 'grape.errors.messages'), parameter: @option
19+
end
20+
end
21+
end
22+
end
23+
end
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require 'spec_helper'
2+
3+
describe Grape::Validations::SameAsValidator do
4+
module ValidationsSpec
5+
module SameAsValidatorSpec
6+
class API < Grape::API
7+
params do
8+
requires :password
9+
requires :password_confirmation, same_as: :password
10+
end
11+
post do
12+
end
13+
14+
params do
15+
requires :password
16+
requires :password_confirmation, same_as: { value: :password, message: 'not match' }
17+
end
18+
post '/custom-message' do
19+
end
20+
end
21+
end
22+
end
23+
24+
def app
25+
ValidationsSpec::SameAsValidatorSpec::API
26+
end
27+
28+
describe '/' do
29+
context 'is the same' do
30+
it do
31+
post '/', password: '987654', password_confirmation: '987654'
32+
expect(last_response.status).to eq(201)
33+
expect(last_response.body).to eq('')
34+
end
35+
end
36+
37+
context 'is not the same' do
38+
it do
39+
post '/', password: '123456', password_confirmation: 'whatever'
40+
expect(last_response.status).to eq(400)
41+
expect(last_response.body).to eq('password_confirmation is not the same as password')
42+
end
43+
end
44+
end
45+
46+
describe '/custom-message' do
47+
context 'is the same' do
48+
it do
49+
post '/custom-message', password: '987654', password_confirmation: '987654'
50+
expect(last_response.status).to eq(201)
51+
expect(last_response.body).to eq('')
52+
end
53+
end
54+
55+
context 'is not the same' do
56+
it do
57+
post '/custom-message', password: '123456', password_confirmation: 'whatever'
58+
expect(last_response.status).to eq(400)
59+
expect(last_response.body).to eq('password_confirmation not match')
60+
end
61+
end
62+
end
63+
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
config.include Rack::Test::Methods
2121
config.include Spec::Support::Helpers
2222
config.raise_errors_for_deprecations!
23+
config.filter_run_when_matching :focus
2324

2425
config.before(:each) { Grape::Util::InheritableSetting.reset_global! }
2526
end

0 commit comments

Comments
 (0)