diff --git a/CHANGELOG.md b/CHANGELOG.md index 29e641bb09..4a358f996e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * [#1740](https://github.com/ruby-grape/grape/pull/1740): Fix dependent parameter validation using `given` when parameter is a `Hash` - [@jvortmann](https://github.com/jvortmann). * Your contribution here. +* [#1737](https://github.com/ruby-grape/grape/pull/1737): Fix translating error when passing symbols as params in custom validations - [@mlzhuyi](https://github.com/mlzhuyi). ### 1.0.2 (1/10/2018) diff --git a/lib/grape/exceptions/base.rb b/lib/grape/exceptions/base.rb index fd4c6a867b..d54fe6efea 100644 --- a/lib/grape/exceptions/base.rb +++ b/lib/grape/exceptions/base.rb @@ -71,6 +71,8 @@ def translate_message(key, **options) end def translate(key, **options) + options = options.dup + options[:default] &&= options[:default].to_s message = ::I18n.translate(key, **options) message.present? ? message : ::I18n.translate(key, locale: FALLBACK_LOCALE, **options) end diff --git a/spec/grape/exceptions/validation_errors_spec.rb b/spec/grape/exceptions/validation_errors_spec.rb index bbbcc10cc9..cbf1a1853a 100644 --- a/spec/grape/exceptions/validation_errors_spec.rb +++ b/spec/grape/exceptions/validation_errors_spec.rb @@ -43,6 +43,15 @@ expect(subject).to contain_exactly('id is missing', 'name is missing') end end + + context 'when attributes is an array of symbols' do + let(:validation_error) { Grape::Exceptions::Validation.new(params: [:admin_field], message: 'Can not set admin-only field') } + subject { described_class.new(errors: [validation_error]).full_messages } + + it 'returns an array with an error full message' do + expect(subject.first).to eq('admin_field Can not set admin-only field') + end + end end context 'api' do