Skip to content

Support for ActiveRecord "base" error serialization #131

Description

@mecampbellsoup

Hey guys - great work with this library!

The JSONAPI spec mentions that error object source pointers can point to a specific attribute, or the "associated entity", per:

source: an object containing references to the source of the error, optionally including any of the following members:
	pointer: a JSON Pointer [RFC6901] to the associated entity in the request 		document [e.g. "/data" for a primary data object, or "/data/attributes/title" for 	a specific attribute].

Often times in our Rails models we do "generic" validations that are usually Ruby versions of SQL uniqueness constraints with some extra logic built-in, e.g.:

class PhoneNumber < ApplicationRecord
...
  def only_one_unverified_number_per_user
    if user
      unverified_excluding_self = user.phone_numbers.unverified.where.not(id: self.id)
      if unverified_excluding_self.present?
        errors.add(:base, "cannot add another unverified phone number")
      end
    end
  end

I think that the behavior of JSONAPI::Serializer.serialize_errors(record.errors) when encountering a "base" error (ie, one produced by doing errors.add(:base, 'something invalid')) should generate a pointer to just /data, the "associated entity" described in the JSONAPI spec.

What do you think? Is there an easy way I could overload the default behavior which sets a pointer of /data/attributes/base?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions