Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/jsonapi/rails/serializable_active_model_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module JSONAPI
module Rails
# @private
class SerializableActiveModelError < Serializable::Error
code do
@code
end

title do
"Invalid #{@field}" unless @field.nil?
end
Expand All @@ -26,10 +30,13 @@ def initialize(exposures)

def as_jsonapi
@errors.keys.flat_map do |key|
@errors.full_messages_for(key).map do |message|
SerializableActiveModelError.new(field: key, message: message,
pointer: @reverse_mapping[key])
.as_jsonapi
@errors.full_messages_for(key).map.with_index do |message, i|
SerializableActiveModelError.new(
field: key,
message: message,
pointer: @reverse_mapping[key],
code: @errors.details[key][i][:error]
).as_jsonapi
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/render_jsonapi_errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
{
'errors' => [
{
'code' => 'blank',
'detail' => 'Name can\'t be blank',
'title' => 'Invalid name',
'source' => { 'pointer' => '/data/attributes/name' }
},
{
'code' => 'invalid',
'detail' => 'Email must be a valid email',
'title' => 'Invalid email',
'source' => { 'pointer' => '/data/attributes/email' }
Expand Down Expand Up @@ -64,11 +66,13 @@ def jsonapi_pointers
def create
errors = [
{
'code' => 'blank',
detail: 'Name can\'t be blank',
title: 'Invalid name',
source: { pointer: '/data/attributes/name' }
},
{
'code' => 'invalid',
detail: 'Email must be a valid email',
title: 'Invalid email',
source: { pointer: '/data/attributes/email' }
Expand Down