Exception: JsonSchema::JsonApiMissingAttribute

Inherits:
StandardError
  • Object
show all
Defined in:
lib/json_schema/json_api_missing_attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(details) ⇒ JsonApiMissingAttribute

Returns a new instance of JsonApiMissingAttribute.



7
8
9
10
# File 'lib/json_schema/json_api_missing_attribute.rb', line 7

def initialize(details)
  @code = 422
  @details = details
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



5
6
7
# File 'lib/json_schema/json_api_missing_attribute.rb', line 5

def code
  @code
end

#detailsObject

Returns the value of attribute details.



5
6
7
# File 'lib/json_schema/json_api_missing_attribute.rb', line 5

def details
  @details
end

Instance Method Details

#build_error(detail) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/json_schema/json_api_missing_attribute.rb', line 26

def build_error(detail)
  {
    status: 422,
    detail: to_human(detail),
    source: to_source(detail)
  }
end

#build_required_errors(required_error) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/json_schema/json_api_missing_attribute.rb', line 34

def build_required_errors(required_error)
  required_error['details']['missing_keys'].map do |missing_key|
    build_error(
      'type' => 'required',
      'data_pointer' => required_error['data_pointer'],
      'details' => { 'missing_key' => missing_key }
    )
  end
end

#required_error?(detail) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/json_schema/json_api_missing_attribute.rb', line 44

def required_error?(detail)
  detail['type'] == 'required'
end

#to_human(detail) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/json_schema/json_api_missing_attribute.rb', line 12

def to_human(detail)
  if detail['type'] == 'required'
    "The property #{to_source(detail)} did not contain the required key #{detail['details']['missing_key']}"
  elsif detail['type'] == 'schema' && detail['schema_pointer'].end_with?('additionalProperties')
    "The property #{to_source(detail)} is not defined on the schema. Additional properties are not allowed"
  else
    "The property #{detail['data_pointer']} did not match the following requirements: #{detail['schema']}"
  end
end

#to_json_apiObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/json_schema/json_api_missing_attribute.rb', line 48

def to_json_api
  required_errors = details.select { |detail| required_error?(detail) }
  other_errors = details.reject { |detail| required_error?(detail) }
  errors = []
  unless required_errors.empty?
    errors.concat(required_errors.map { |error| build_required_errors(error) }.reduce(:concat))
  end
  errors.concat(other_errors.map { |error| build_error(error) })
  { errors: }
end

#to_source(detail) ⇒ Object



22
23
24
# File 'lib/json_schema/json_api_missing_attribute.rb', line 22

def to_source(detail)
  detail['data_pointer'].empty? ? '/' : detail['data_pointer']
end