Class: OpenapiFirst::DefaultErrorResponse

Inherits:
ErrorResponse show all
Defined in:
lib/openapi_first/default_error_response.rb

Instance Attribute Summary

Attributes inherited from ErrorResponse

#data, #location, #schema, #status, #title, #validation_output

Instance Method Summary collapse

Methods inherited from ErrorResponse

#content_type, #initialize, #render

Constructor Details

This class inherits a constructor from OpenapiFirst::ErrorResponse

Instance Method Details

#bodyObject



7
8
9
# File 'lib/openapi_first/default_error_response.rb', line 7

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

#default_errorsObject



23
24
25
26
27
28
# File 'lib/openapi_first/default_error_response.rb', line 23

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

#pointer(data_pointer) ⇒ Object



41
42
43
44
45
# File 'lib/openapi_first/default_error_response.rb', line 41

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

  data_pointer.delete_prefix('/')
end

#pointer_keyObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/openapi_first/default_error_response.rb', line 30

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

#serialized_errorsObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/openapi_first/default_error_response.rb', line 11

def serialized_errors
  return default_errors unless validation_output

  key = pointer_key
  [
    {
      source: { key => pointer(validation_output['instanceLocation']) },
      title: validation_output['error']
    }
  ]
end