Sha256: a3e902e5ac5bebefbc5a535828ffbccc4e8c29e2e5664fa49e0ec04ec881dec9

Contents?: true

Size: 1.08 KB

Versions: 5

Compression:

Stored size: 1.08 KB

Contents

require 'jsonapi/error_serializer'

module JSONAPI
  # [ActiveModel::Errors] serializer
  class ActiveModelErrorSerializer < ErrorSerializer
    set_id :object_id
    set_type :error

    attribute :status do
      '422'
    end

    attribute :title do
      Net::HTTP::STATUS_CODES[422]
    end

    attribute :code do |object|
      _, error_hash = object
      error_hash[:error]
    end

    attribute :detail do |object, params|
      error_key, error_hash = object
      errors_object = params[:model].errors
      message = errors_object.generate_message(error_key, error_hash[:error])
      errors_object.full_message(error_key, message)
    end

    attribute :source do |object, params|
      error_key, _ = object
      model_serializer = params[:model_serializer]

      if model_serializer.attributes_to_serialize.keys.include?(error_key)
        { pointer: "/data/attributes/#{error_key}" }
      elsif model_serializer.relationships_to_serialize.keys.include?(error_key)
        { pointer: "/data/relationships/#{error_key}" }
      else
        { pointer: '' }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
jsonapi.rb-1.1.3 lib/jsonapi/active_model_error_serializer.rb
jsonapi.rb-1.1.2 lib/jsonapi/active_model_error_serializer.rb
jsonapi.rb-1.1.1 lib/jsonapi/active_model_error_serializer.rb
jsonapi.rb-1.1.0 lib/jsonapi/active_model_error_serializer.rb
jsonapi.rb-1.0.0 lib/jsonapi/active_model_error_serializer.rb