Skip to content

Return error codes #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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