Class: OpenapiFirst::ErrorResponses::JsonApi

Inherits:
OpenapiFirst::ErrorResponse show all
Defined in:
lib/openapi_first/error_responses/json_api.rb

Instance Attribute Summary

Attributes inherited from OpenapiFirst::ErrorResponse

#env, #request_validation_error

Instance Method Summary collapse

Methods inherited from OpenapiFirst::ErrorResponse

#data, #initialize, #message, #render, #schema, #validation_output

Constructor Details

This class inherits a constructor from OpenapiFirst::ErrorResponse

Instance Method Details

#bodyObject



8
9
10
# File 'lib/openapi_first/error_responses/json_api.rb', line 8

def body
  MultiJson.dump({ errors: serialized_errors })
end

#content_typeObject



12
13
14
# File 'lib/openapi_first/error_responses/json_api.rb', line 12

def content_type
  'application/vnd.api+json'
end

#default_errorsObject



33
34
35
36
37
38
# File 'lib/openapi_first/error_responses/json_api.rb', line 33

def default_errors
  [{
    status: status.to_s,
    title: message
  }]
end

#pointer(data_pointer) ⇒ Object



51
52
53
54
55
# File 'lib/openapi_first/error_responses/json_api.rb', line 51

def pointer(data_pointer)
  return data_pointer if location == :body

  data_pointer.delete_prefix('/')
end

#pointer_keyObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/openapi_first/error_responses/json_api.rb', line 40

def pointer_key
  case location
  when :body
    :pointer
  when :query, :path
    :parameter
  else
    location
  end
end

#serialized_errorsObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/openapi_first/error_responses/json_api.rb', line 16

def serialized_errors
  return default_errors unless validation_output

  key = pointer_key
  validation_errors&.map do |error|
    {
      status: status.to_s,
      source: { key => pointer(error['instanceLocation']) },
      title: error['error']
    }
  end
end

#validation_errorsObject



29
30
31
# File 'lib/openapi_first/error_responses/json_api.rb', line 29

def validation_errors
  validation_output['errors'] || [validation_output]
end