Skip to Content Skip to Search
Methods
#
S

Class Public methods

_serialize_active_model(errors)

# File app/serializers/v2/error_serializer.rb, line 47
def self._serialize_active_model(errors)
  field_errors = {}

  errors.attribute_names.each do |attribute|
    key = attribute.to_s.camelize(:lower)

    # We are only sending the first error for a given attribute.
    # If there are multiple errors, they will be fixed as they
    # do subsquent submissions.
    error = errors.full_messages_for(attribute).first

    field_errors[key] = {
      id: "errors.format.#{key}",
      defaultMessage: error
    }
  end

  field_errors
end

_serialize_hash(errors)

# File app/serializers/v2/error_serializer.rb, line 23
def self._serialize_hash(errors)
  field_errors = {}

  errors.keys.each do |key|
    # We are only sending the first error for a given attribute.
    # If there are multiple errors, they will be fixed as they
    # do subsquent submissions.
    error = if errors[key].is_a?(Array)
      errors[key].first
    else
      errors[key]
    end

    key = key.to_s.camelize(:lower)

    field_errors[key] = {
      id: "errors.format.#{key}",
      defaultMessage: error
    }
  end

  field_errors
end

serialize(errors)

{

"id"             => "errors.format.firstName",
"defaultMessage" => "First name can't be blank",

},

# File app/serializers/v2/error_serializer.rb, line 13
def self.serialize(errors)
  return if errors.nil?

  if errors.is_a?(Hash)
    _serialize_hash(errors)
  elsif errors.is_a?(ActiveModel::Errors)
    _serialize_active_model(errors)
  end
end