Skip to content

Commit b8d8712

Browse files
author
Marieke GUEYE
committed
Store back message_key on Grape::Exceptions::Validation
1 parent 0331563 commit b8d8712

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
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 back `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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Validation < Grape::Exceptions::Base
99
def initialize(args = {})
1010
fail 'Params are missing:' unless args.key? :params
1111
@params = args[:params]
12+
@message_key = args[:message] if args.key?(:message) && args[:message].is_a?(Symbol)
1213
args[:message] = translate_message(args[:message]) if args.key? :message
1314
super
1415
end

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)