Skip to content

Commit 18c9d4b

Browse files
authored
Merge pull request #1737 from mlzhuyi/ensure_default_option_a_string_when_call_i18.t
Ensure the value of default option must be a string when calling I18n.t
2 parents 1dd02dd + a23ffad commit 18c9d4b

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
@@ -9,6 +9,7 @@
99
* [#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).
1010

1111
* Your contribution here.
12+
* [#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).
1213

1314
### 1.0.2 (1/10/2018)
1415

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)