-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b368380
commit 2efbd01
Showing
7 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters