Skip to content

Commit

Permalink
Store message_key on Grape::Exceptions::Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
stjhimy committed Dec 11, 2015
1 parent a00b2d8 commit 6b3304f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
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

0 comments on commit 6b3304f

Please sign in to comment.