Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* [#2726](https://github.com/ruby-grape/grape/pull/2726): Reuse one `AttributesIterator` per validator and drop the unused `Enumerable` mixin - [@ericproulx](https://github.com/ericproulx).
* [#2728](https://github.com/ruby-grape/grape/pull/2728): Deprecate passing a positional options Hash to `auth`/`http_basic`/`http_digest`; pass keyword arguments instead - [@ericproulx](https://github.com/ericproulx).
* [#2733](https://github.com/ruby-grape/grape/pull/2733): Drop the dead `active_support/core_ext/hash/reverse_merge` require; call `ActiveSupport::HashWithIndifferentAccess.new(...)` directly at call sites - [@ericproulx](https://github.com/ericproulx).
* [#2747](https://github.com/ruby-grape/grape/pull/2747): Drop `Enumerable` from `Grape::Exceptions::ValidationErrors` and remove its public `#each`; rewrite `#full_messages` to walk `#errors` directly - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

#### Fixes
Expand Down
20 changes: 20 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@ Upgrading Grape

### Upgrading to >= 3.3

#### `Grape::Exceptions::ValidationErrors` no longer mixes in `Enumerable`

`Grape::Exceptions::ValidationErrors` no longer includes `Enumerable` and no longer defines a public `#each`. The Enumerable surface (`#each`, `#map`, `#select`, `#to_a`, etc.) was undocumented and untested; the documented accessors — `#errors`, `#full_messages`, `#message`, `#as_json` — are unchanged.

If a `rescue_from` block iterated over the exception instance, switch to `#errors`:

```ruby
# before
rescue_from Grape::Exceptions::ValidationErrors do |e|
e.each { |attribute, error| ... }
end

# after
rescue_from Grape::Exceptions::ValidationErrors do |e|
e.errors.each do |attributes, errs|
errs.each { |error| ... }
end
end
```

#### `auth`, `http_basic` and `http_digest` now take keyword arguments

`Grape::Middleware::Auth::DSL#auth`, `#http_basic` and `#http_digest` now accept their options as keyword arguments instead of a positional `Hash`. Calls using bare keyword syntax or a block are unaffected:
Expand Down
28 changes: 10 additions & 18 deletions lib/grape/exceptions/validation_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,13 @@
module Grape
module Exceptions
class ValidationErrors < Base
include Enumerable

attr_reader :errors

def initialize(errors: [], headers: {})
@errors = errors.group_by(&:params)
super(message: full_messages.join(', '), status: 400, headers:)
end

def each
errors.each_pair do |attribute, errors|
errors.each do |error|
yield attribute, error
end
end
end

def as_json(**_opts)
errors.map do |k, v|
{
Expand All @@ -34,14 +24,16 @@ def to_json(*_opts)
end

def full_messages
messages = map do |attributes, error|
translate(
:format,
scope: 'grape.errors',
default: '%<attributes>s %<message>s',
attributes: translate_attributes(attributes),
message: error.message
)
messages = errors.flat_map do |attributes, errs|
errs.map do |error|
translate(
:format,
scope: 'grape.errors',
default: '%<attributes>s %<message>s',
attributes: translate_attributes(attributes),
message: error.message
)
end
end
messages.uniq!
messages
Expand Down
Loading