Skip to content

Commit a7879d0

Browse files
committed
Merge pull request #1064 from romanlehnert/feature/make_full_messages_public
Make Grape::Exceptions::ValidationErrors#full_messages public
2 parents 735034b + 3714481 commit a7879d0

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Next Release
55

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

1011
#### Fixes

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,16 @@ subject.rescue_from Grape::Exceptions::ValidationErrors do |e|
11021102
end
11031103
```
11041104

1105+
`Grape::Exceptions::ValidationErrors#full_messages` returns the validation messages as an array. `Grape::Exceptions::ValidationErrors#message` joins the messages to one string.
1106+
1107+
For responding with an array of validation messages, you can use `Grape::Exceptions::ValidationErrors#full_messages`.
1108+
```ruby
1109+
format :json
1110+
subject.rescue_from Grape::Exceptions::ValidationErrors do |e|
1111+
error!({ messages: e.full_messages }, 400)
1112+
end
1113+
```
1114+
11051115
### I18n
11061116

11071117
Grape supports I18n for parameter-related error messages, but will fallback to English if

lib/grape/exceptions/validation_errors.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ def to_json(_opts = {})
3737
as_json.to_json
3838
end
3939

40-
private
41-
4240
def full_messages
4341
map { |attributes, error| full_message(attributes, error) }.uniq
4442
end
4543

44+
private
45+
4646
def full_message(attributes, error)
4747
I18n.t(
4848
'grape.errors.format'.to_sym,

spec/grape/exceptions/validation_errors_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
end
1818
end
1919

20+
describe '#full_messages' do
21+
context 'with errors' do
22+
let(:validation_error_1) { Grape::Exceptions::Validation.new(params: ['id'], message_key: 'presence') }
23+
let(:validation_error_2) { Grape::Exceptions::Validation.new(params: ['name'], message_key: 'presence') }
24+
subject { described_class.new(errors: [validation_error_1, validation_error_2]).full_messages }
25+
26+
it 'returns an array with each errors full message' do
27+
expect(subject).to contain_exactly('id is missing', 'name is missing')
28+
end
29+
end
30+
end
31+
2032
context 'api' do
2133
subject { Class.new(Grape::API) }
2234

0 commit comments

Comments
 (0)