Sha256: c69b6bd148f3c1adb08a9094ef067aea218cb9255ca0c50c9a2aecc2d18fca5e

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 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.params] ||= []
          @errors[validation_error.params] << 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

      def as_json
        errors.map do |k, v|
          {
            params: k,
            messages: v.map(&:to_s)
          }
        end
      end

      def to_json
        as_json.to_json
      end

      private

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

      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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
grape-0.11.0 lib/grape/exceptions/validation_errors.rb
grape-0.10.1 lib/grape/exceptions/validation_errors.rb
grape-0.10.0 lib/grape/exceptions/validation_errors.rb