Skip to content

Commit 59af167

Browse files
mkoudblock
authored andcommitted
Store message_key on Grape::Exceptions::Validation.
Store `message_key` on `Grape::Exceptions::Validation`.
1 parent 0331563 commit 59af167

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
* Your contribution here.
55

6+
#### Features
7+
* [#1366](https://github.com/ruby-grape/grape/pull/1366): Store `message_key` on `Grape::Exceptions::Validation` - [@mkou](https://github.com/mkou).
8+
69
0.16.2 (4/12/2016)
710
==================
811

lib/grape/exceptions/validation.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ class Validation < Grape::Exceptions::Base
99
def initialize(args = {})
1010
fail 'Params are missing:' unless args.key? :params
1111
@params = args[:params]
12-
args[:message] = translate_message(args[:message]) if args.key? :message
12+
if args.key?(:message)
13+
@message_key = args[:message] if args[:message].is_a?(Symbol)
14+
args[:message] = translate_message(args[:message])
15+
end
1316
super
1417
end
1518

spec/grape/exceptions/validation_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,14 @@
44
it 'fails when params are missing' do
55
expect { Grape::Exceptions::Validation.new(message: 'presence') }.to raise_error(RuntimeError, 'Params are missing:')
66
end
7+
context 'when message is a symbol' do
8+
it 'stores message_key' do
9+
expect(Grape::Exceptions::Validation.new(params: ['id'], message: :presence).message_key).to eq(:presence)
10+
end
11+
end
12+
context 'when message is a String' do
13+
it 'does not store the message_key' do
14+
expect(Grape::Exceptions::Validation.new(params: ['id'], message: 'presence').message_key).to eq(nil)
15+
end
16+
end
717
end

0 commit comments

Comments
 (0)