Sha256: b24ec265a7efdb5f51d690f06a88f7223c607d4bb73e32a14b628aacc4176c87

Contents?: true

Size: 1 KB

Versions: 3

Compression:

Stored size: 1 KB

Contents

require 'grape/exceptions/base'

module Grape
  module Exceptions
    class ValidationErrors < Grape::Exceptions::Base
      include Enumerable

      attr_reader :errors

      def initialize(args = {})
        @errors = {}
        args[:errors].each do |validation_error|
          @errors[validation_error.param] ||= []
          @errors[validation_error.param] << validation_error
        end
        super message: full_messages.join(', '), status: 400
      end

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

      private

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

      def full_message(attribute, error)
        I18n.t(
          "grape.errors.format".to_sym,
          default: "%{attribute} %{message}",
          attribute: translate_attribute(attribute),
          message: error.message
        )
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
grape-security-0.8.0 lib/grape/exceptions/validation_errors.rb
grape-0.8.0 lib/grape/exceptions/validation_errors.rb
grape-0.7.0 lib/grape/exceptions/validation_errors.rb