class Grape::Exceptions::ValidationErrors

Constants

DEFAULT_ERRORS_FORMAT
ERRORS_FORMAT_KEY

Attributes

errors[R]

Public Class Methods

new(errors: [], headers: {}, **_options) click to toggle source
Calls superclass method Grape::Exceptions::Base::new
# File lib/grape/exceptions/validation_errors.rb, line 13
def initialize(errors: [], headers: {}, **_options)
  @errors = errors.group_by(&:params)
  super(message: full_messages.join(', '), status: 400, headers: headers)
end

Public Instance Methods

as_json(**_opts) click to toggle source
# File lib/grape/exceptions/validation_errors.rb, line 26
def as_json(**_opts)
  errors.map do |k, v|
    {
      params: k,
      messages: v.map(&:to_s)
    }
  end
end
each() { |attribute, error| ... } click to toggle source
# File lib/grape/exceptions/validation_errors.rb, line 18
def each
  errors.each_pair do |attribute, errors|
    errors.each do |error|
      yield attribute, error
    end
  end
end
full_messages() click to toggle source
# File lib/grape/exceptions/validation_errors.rb, line 39
def full_messages
  messages = map do |attributes, error|
    I18n.t(
      ERRORS_FORMAT_KEY,
      default: DEFAULT_ERRORS_FORMAT,
      attributes: translate_attributes(attributes),
      message: error.message
    )
  end
  messages.uniq!
  messages
end
to_json(*_opts) click to toggle source
# File lib/grape/exceptions/validation_errors.rb, line 35
def to_json(*_opts)
  as_json.to_json
end