Skip to content

Commit a23ffad

Browse files
author
yi.zhu
committed
Ensure the value of default option must be a string when calling I18n.t
1 parent 16c6662 commit a23ffad

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#### Fixes
88

99
* Your contribution here.
10+
* [#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).
1011

1112
### 1.0.2 (1/10/2018)
1213

lib/grape/exceptions/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ def translate_message(key, **options)
7171
end
7272

7373
def translate(key, **options)
74+
options = options.dup
75+
options[:default] &&= options[:default].to_s
7476
message = ::I18n.translate(key, **options)
7577
message.present? ? message : ::I18n.translate(key, locale: FALLBACK_LOCALE, **options)
7678
end

spec/grape/exceptions/validation_errors_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@
4343
expect(subject).to contain_exactly('id is missing', 'name is missing')
4444
end
4545
end
46+
47+
context 'when attributes is an array of symbols' do
48+
let(:validation_error) { Grape::Exceptions::Validation.new(params: [:admin_field], message: 'Can not set admin-only field') }
49+
subject { described_class.new(errors: [validation_error]).full_messages }
50+
51+
it 'returns an array with an error full message' do
52+
expect(subject.first).to eq('admin_field Can not set admin-only field')
53+
end
54+
end
4655
end
4756

4857
context 'api' do

0 commit comments

Comments
 (0)