Class: JsonApiServer::ValidationErrors

Inherits:
Object
  • Object
show all
Includes:
ApiVersion, Serializer
Defined in:
lib/json_api_server/validation_errors.rb

Overview

Converts ActiveModel validations to JSON API Errors. Spec: jsonapi.org/format/#error-objects.

Calling render_422(model_instance) in a controller will create an instance of this class and render with appropriate headers.

i.e,

def create
  topic = Topic.new(topic_params)

  if topic.save
   serializer = TopicSerializer.new(topic)
   render json: serializer.to_json, status: :created
 else
   render_422(topic)
 end
end

Instance Method Summary collapse

Methods included from ApiVersion

#jsonapi

Methods included from Serializer

#serializer_options, #to_json

Constructor Details

#initialize(model) ⇒ ValidationErrors

Returns a new instance of ValidationErrors.



24
25
26
27
# File 'lib/json_api_server/validation_errors.rb', line 24

def initialize(model)
  errors = get_errors(model)
  @errors = JsonApiServer::Errors.new(errors)
end

Instance Method Details

#as_jsonObject



29
30
31
# File 'lib/json_api_server/validation_errors.rb', line 29

def as_json
  @errors.as_json
end