Class: FunWithJsonApi::SchemaValidators::CheckDocumentIdMatchesResource

Inherits:
Object
  • Object
show all
Defined in:
lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_validator) ⇒ CheckDocumentIdMatchesResource

Returns a new instance of CheckDocumentIdMatchesResource.



16
17
18
# File 'lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb', line 16

def initialize(schema_validator)
  @schema_validator = schema_validator
end

Instance Attribute Details

#schema_validatorObject (readonly)

Returns the value of attribute schema_validator.



8
9
10
# File 'lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb', line 8

def schema_validator
  @schema_validator
end

Class Method Details

.call(schema_validator) ⇒ Object



4
5
6
# File 'lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb', line 4

def self.call(schema_validator)
  new(schema_validator).call
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb', line 20

def call
  if resource.try(:persisted?)
    # Ensure correct update document is being sent
    check_resource_id_is_a_string
    check_resource_id_matches_document_id
  elsif document_id
    # Ensure correct create document is being sent
    check_resource_id_is_a_string
    check_resource_id_can_be_client_generated
    check_resource_id_has_not_already_been_used
  end
end

#check_resource_id_can_be_client_generatedObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb', line 55

def check_resource_id_can_be_client_generated
  # Ensure id has been provided as an attribute
  if deserializer.attributes.none? { |attribute| attribute.name == :id }
    deserializer_name = deserializer.class.name || 'Deserializer'
    message = "id parameter for '#{resource_type}' cannot be set"\
              " as it has not been defined as a #{deserializer_name} attribute"
    payload = ExceptionPayload.new(
      detail: resource_id_can_not_be_client_generated_message
    )
    raise Exceptions::IllegalClientGeneratedIdentifier.new(message, payload)
  end
end

#check_resource_id_has_not_already_been_usedObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb', line 68

def check_resource_id_has_not_already_been_used
  if (existing = deserializer.load_resource_from_id_value(document_id))
    deserializer_class = deserializer.class.name || 'Deserializer'
    message = "#{deserializer_class}#load_resource_from_id_value for '#{resource_type}' has"\
              ' found a existing resource matching document id'\
              ": #{existing.class.name}##{existing.id}"
    payload = ExceptionPayload.new(
      detail: resource_id_has_already_been_used_message
    )
    raise Exceptions::InvalidClientGeneratedIdentifier.new(message, payload)
  end
end

#check_resource_id_is_a_stringObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb', line 33

def check_resource_id_is_a_string
  unless document_id.is_a?(String)
    payload = ExceptionPayload.new(
      detail: document_id_is_not_a_string_message,
      pointer: '/data/id'
    )
    message = "document id is not a string: #{document_id.class.name}"
    raise Exceptions::InvalidDocumentIdentifier.new(message, payload)
  end
end

#check_resource_id_matches_document_idObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/fun_with_json_api/schema_validators/check_document_id_matches_resource.rb', line 44

def check_resource_id_matches_document_id
  if document_id != resource_id
    message = "resource id '#{resource_id}' does not match the expected id for"\
              " '#{resource_type}': '#{document_id}'"
    payload = ExceptionPayload.new(
      detail: document_id_does_not_match_resource_message
    )
    raise Exceptions::InvalidDocumentIdentifier.new(message, payload)
  end
end