Skip to content

Commit

Permalink
Merge pull request #1064 from romanlehnert/feature/make_full_messages…
Browse files Browse the repository at this point in the history
…_public

Make Grape::Exceptions::ValidationErrors#full_messages public
  • Loading branch information
dblock committed Jul 13, 2015
2 parents 735034b + 3714481 commit a7879d0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Next Release

* [#1039](https://github.com/intridea/grape/pull/1039): Added support for custom parameter types - [@rnubel](https://github.com/rnubel).
* [#1047](https://github.com/intridea/grape/pull/1047): Adds `given` to DSL::Parameters, allowing for dependent params - [@rnubel](https://github.com/rnubel).
* [#1064](https://github.com/intridea/grape/pull/1064): Add public `Grape::Exception::ValidationErrors#full_messages` - [@romanlehnert](https://github.com/romanlehnert).
* Your contribution here!

#### Fixes
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,16 @@ subject.rescue_from Grape::Exceptions::ValidationErrors do |e|
end
```

`Grape::Exceptions::ValidationErrors#full_messages` returns the validation messages as an array. `Grape::Exceptions::ValidationErrors#message` joins the messages to one string.

For responding with an array of validation messages, you can use `Grape::Exceptions::ValidationErrors#full_messages`.
```ruby
format :json
subject.rescue_from Grape::Exceptions::ValidationErrors do |e|
error!({ messages: e.full_messages }, 400)
end
```

### I18n

Grape supports I18n for parameter-related error messages, but will fallback to English if
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/exceptions/validation_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def to_json(_opts = {})
as_json.to_json
end

private

def full_messages
map { |attributes, error| full_message(attributes, error) }.uniq
end

private

def full_message(attributes, error)
I18n.t(
'grape.errors.format'.to_sym,
Expand Down
12 changes: 12 additions & 0 deletions spec/grape/exceptions/validation_errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
end
end

describe '#full_messages' do
context 'with errors' do
let(:validation_error_1) { Grape::Exceptions::Validation.new(params: ['id'], message_key: 'presence') }
let(:validation_error_2) { Grape::Exceptions::Validation.new(params: ['name'], message_key: 'presence') }
subject { described_class.new(errors: [validation_error_1, validation_error_2]).full_messages }

it 'returns an array with each errors full message' do
expect(subject).to contain_exactly('id is missing', 'name is missing')
end
end
end

context 'api' do
subject { Class.new(Grape::API) }

Expand Down

0 comments on commit a7879d0

Please sign in to comment.