class Grape::Exceptions::ValidationErrors

Attributes

errors[R]

Public Class Methods

new(args = {}) click to toggle source
Calls superclass method Grape::Exceptions::Base.new
# File lib/grape/exceptions/validation_errors.rb, line 10
def initialize(args = {})
  @errors = {}
  args[:errors].each do |validation_error|
    @errors[validation_error.params] ||= []
    @errors[validation_error.params] << validation_error
  end

  super message: full_messages.join(', '), status: 400, headers: args[:headers]
end

Public Instance Methods

as_json(_opts = {}) click to toggle source
# File lib/grape/exceptions/validation_errors.rb, line 28
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 20
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 41
def full_messages
  map { |attributes, error| full_message(attributes, error) }.uniq
end
to_json(_opts = {}) click to toggle source
# File lib/grape/exceptions/validation_errors.rb, line 37
def to_json(_opts = {})
  as_json.to_json
end

Private Instance Methods

full_message(attributes, error) click to toggle source
# File lib/grape/exceptions/validation_errors.rb, line 47
def full_message(attributes, error)
  I18n.t(
    'grape.errors.format'.to_sym,
    default: '%{attributes} %{message}',
    attributes: attributes.count == 1 ? translate_attribute(attributes.first) : translate_attributes(attributes),
    message: error.message
  )
end