Sha256: a103b70053094d624a987cc4755420437c44a9dc6ae07616a15183e632b77a28

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 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
      Rack::Utils::HTTP_STATUS_CODES[422]
    end

    attribute :code do |object|
      _, error_hash = object
      code = error_hash[:error] unless error_hash[:error].is_a?(Hash)
      code ||= error_hash[:message] || :invalid
      # `parameterize` separator arguments are different on Rails 4 vs 5...
      code.to_s.delete("''").parameterize.gsub('-', '_')
    end

    attribute :detail do |object, params|
      error_key, error_hash = object
      errors_object = params[:model].errors

      # Rails 4 provides just the message.
      if error_hash[:error].present? && error_hash[:error].is_a?(Hash)
        message = errors_object.generate_message(
          error_key, nil, error_hash[:error]
        )
      elsif error_hash[:error].present?
        message = errors_object.generate_message(error_key, error_hash[:error])
      else
        message = error_hash[:message]
      end

      errors_object.full_message(error_key, message)
    end

    attribute :source do |object, params|
      error_key, _ = object
      model_serializer = params[:model_serializer]
      attrs = (model_serializer.attributes_to_serialize || {}).keys
      rels = (model_serializer.relationships_to_serialize || {}).keys

      if attrs.include?(error_key)
        { pointer: "/data/attributes/#{error_key}" }
      elsif rels.include?(error_key)
        { pointer: "/data/relationships/#{error_key}" }
      else
        { pointer: '' }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jsonapi.rb-1.4.4 lib/jsonapi/active_model_error_serializer.rb
jsonapi.rb-1.4.3 lib/jsonapi/active_model_error_serializer.rb
jsonapi.rb-1.4.2 lib/jsonapi/active_model_error_serializer.rb