Skip to content

Commit

Permalink
Ensure the value of default option must be a string when calling I18n.t
Browse files Browse the repository at this point in the history
  • Loading branch information
yi.zhu committed Feb 11, 2018
1 parent 16c6662 commit a23ffad
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#### Fixes

* 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)

Expand Down
2 changes: 2 additions & 0 deletions lib/grape/exceptions/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/grape/exceptions/validation_errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a23ffad

Please sign in to comment.