Add useful validation message for is_equal_to

This commit is contained in:
Eric Schultz 2018-12-13 12:42:27 -06:00
parent 33a280a0d1
commit 3b3763f5a6
3 changed files with 24 additions and 1 deletions

View file

@ -2,7 +2,7 @@
class Houdini::V1::Validators::IsEqualTo < Grape::Validations::Base
def validate_param!(attr_name, params)
if params[attr_name] != params[@option]
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: 'MESSAGE'
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name), @scope.full_name(@option)], message: message(:is_equal_to)
end
end
end

View file

@ -0,0 +1,7 @@
# License: CC0-1.0
# grape validation errors require a very specific format for their translation messages. Why? *shrug*
en:
grape:
errors:
messages:
is_equal_to: "must be the same"

View file

@ -82,6 +82,22 @@ describe Houdini::V1::Nonprofit, :type => :controller do
expect_validation_errors(JSON.parse(response.body), expected)
end
it 'should reject unmatching passwords ' do
input = {
user: {
email: "wmeil@email.com",
name: "name",
password: 'password',
password_confirmation: 'doesn\'t match'
}
}
xhr :post, '/api/v1/nonprofit', input
expect(response.code).to eq "400"
expect(JSON.parse(response.body)['errors']).to include(h(params:["user[password]", "user[password_confirmation]"], messages: gr_e("is_equal_to")))
end
it 'attempts to make a slug copy and returns the proper errors' do
force_create(:nonprofit, slug: "n", state_code_slug: "wi", city_slug: "appleton")
input = {