Skip to content

Store message_key on validation exception #1227

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

Closed
wants to merge 1 commit into from
Closed
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 @@

* [#1216](https://github.com/ruby-grape/grape/pull/1142): Fix JSON error response when calling `error!` with non-Strings - [@jrforrest](https://github.com/jrforrest).
* [#1225](https://github.com/ruby-grape/grape/pull/1225): Fix `given` with nested params not returning correct declared params - [@JanStevens](https://github.com/JanStevens).
* [#1227](https://github.com/ruby-grape/grape/pull/1227): Store `message_key` on Grape::Exceptions::Validation - [@stjhimy](https://github.com/sthimy).
* Your contribution here.

0.14.0 (12/07/2015)
Expand Down
2 changes: 2 additions & 0 deletions lib/grape/exceptions/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ module Grape
module Exceptions
class Validation < Grape::Exceptions::Base
attr_accessor :params
attr_accessor :message_key

def initialize(args = {})
fail 'Params are missing:' unless args.key? :params
@params = args[:params]
@message_key = args[:message_key]
args[:message] = translate_message(args[:message_key]) if args.key? :message_key
super
end
Expand Down
11 changes: 11 additions & 0 deletions spec/grape/exceptions/validation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'spec_helper'

describe Grape::Exceptions::Validation do
it 'fails when params are missing' do
expect { Grape::Exceptions::Validation.new(message_key: 'presence') }.to raise_error(RuntimeError, 'Params are missing:')
end

it 'store message_key' do
expect(Grape::Exceptions::Validation.new(params: ['id'], message_key: 'presence').message_key).to eq('presence')
end
end