From 3b3763f5a6d71a89261b8eac7c410c7a2d00e46f Mon Sep 17 00:00:00 2001 From: Eric Schultz Date: Thu, 13 Dec 2018 12:42:27 -0600 Subject: [PATCH] Add useful validation message for is_equal_to --- app/api/houdini/v1/validators/is_equal_to.rb | 2 +- config/locales/grape.en.yml | 7 +++++++ spec/api/houdini/nonprofit_spec.rb | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 config/locales/grape.en.yml diff --git a/app/api/houdini/v1/validators/is_equal_to.rb b/app/api/houdini/v1/validators/is_equal_to.rb index e7509d58..c84b8075 100644 --- a/app/api/houdini/v1/validators/is_equal_to.rb +++ b/app/api/houdini/v1/validators/is_equal_to.rb @@ -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 \ No newline at end of file diff --git a/config/locales/grape.en.yml b/config/locales/grape.en.yml new file mode 100644 index 00000000..8e7ad93f --- /dev/null +++ b/config/locales/grape.en.yml @@ -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" \ No newline at end of file diff --git a/spec/api/houdini/nonprofit_spec.rb b/spec/api/houdini/nonprofit_spec.rb index f3e51aec..daddc8ba 100644 --- a/spec/api/houdini/nonprofit_spec.rb +++ b/spec/api/houdini/nonprofit_spec.rb @@ -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 = {