diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cac716ee..ef72aa6d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/grape/exceptions/validation.rb b/lib/grape/exceptions/validation.rb index 3e598c749..def15de8b 100644 --- a/lib/grape/exceptions/validation.rb +++ b/lib/grape/exceptions/validation.rb @@ -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 diff --git a/spec/grape/exceptions/validation_spec.rb b/spec/grape/exceptions/validation_spec.rb new file mode 100644 index 000000000..f0c02450f --- /dev/null +++ b/spec/grape/exceptions/validation_spec.rb @@ -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