Skip to content

Commit

Permalink
Merge pull request #1737 from mlzhuyi/ensure_default_option_a_string_…
Browse files Browse the repository at this point in the history
…when_call_i18.t

Ensure the value of default option must be a string when calling I18n.t
  • Loading branch information
dblock authored Feb 14, 2018
2 parents 1dd02dd + a23ffad commit 18c9d4b
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 @@ -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)

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 18c9d4b

Please sign in to comment.