class FunWithJsonApi::SchemaValidators::CheckRelationships

Attributes

deserializer[R]
document[R]

Public Class Methods

call(document, deserializer) click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationships.rb, line 6
def self.call(document, deserializer)
  new(document, deserializer).call
end
new(document, deserializer) click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationships.rb, line 13
def initialize(document, deserializer)
  @document = document
  @deserializer = deserializer
end

Public Instance Methods

call() click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationships.rb, line 18
def call
  relationships = document['data'].fetch('relationships', {})

  CheckRelationshipNames.call(document, deserializer, relationships.keys)

  check_for_invalid_relationship_type! relationships

  true
end
check_for_invalid_relationship_type!(relationships_hash) click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationships.rb, line 28
def check_for_invalid_relationship_type!(relationships_hash)
  payload = build_invalid_relationship_type_payload(relationships_hash)
  return if payload.empty?

  message = 'A relationship received data with an incorrect type'
  raise FunWithJsonApi::Exceptions::InvalidRelationshipType.new(message, payload)
end
check_for_invalid_relationship_type_in_collection!(relationship, collection_data) click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationships.rb, line 36
def check_for_invalid_relationship_type_in_collection!(relationship, collection_data)
  return unless collection_data.is_a?(Array)

  collection_data.each_with_index.map do |item, index|
    next if item['type'] == relationship.type

    build_invalid_collection_item_payload(relationship, index)
  end
end
check_for_invalid_relationship_type_in_relationship!(relationship, relationship_data) click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationships.rb, line 46
def check_for_invalid_relationship_type_in_relationship!(relationship, relationship_data)
  return unless relationship_data.is_a?(Hash)
  return if relationship_data['type'] == relationship.type

  build_invalid_relationship_item_payload(relationship)
end

Private Instance Methods

build_invalid_collection_item_payload(relationship, index) click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationships.rb, line 84
def build_invalid_collection_item_payload(relationship, index)
  ExceptionPayload.new(
    detail: invalid_relationship_type_in_array_message(relationship),
    pointer: "/data/relationships/#{relationship.name}/data/#{index}/type"
  )
end
build_invalid_relationship_item_payload(relationship) click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationships.rb, line 91
def build_invalid_relationship_item_payload(relationship)
  ExceptionPayload.new(
    detail: invalid_relationship_type_in_hash_message(relationship),
    pointer: "/data/relationships/#{relationship.name}/data/type"
  )
end
build_invalid_relationship_type_payload(relationships_hash) click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationships.rb, line 73
def build_invalid_relationship_type_payload(relationships_hash)
  deserializer.relationships.map do |relationship|
    data = relationships_hash.fetch(relationship.name.to_s, 'data' => nil)['data']
    if relationship.has_many?
      check_for_invalid_relationship_type_in_collection!(relationship, data)
    else
      check_for_invalid_relationship_type_in_relationship!(relationship, data)
    end
  end.flatten.compact
end
invalid_relationship_type_in_array_message(relationship) click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationships.rb, line 55
def invalid_relationship_type_in_array_message(relationship)
  I18n.t(
    :invalid_relationship_type_in_array,
    relationship: relationship.name,
    relationship_type: relationship.type,
    scope: 'fun_with_json_api.schema_validators'
  )
end
invalid_relationship_type_in_hash_message(relationship) click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationships.rb, line 64
def invalid_relationship_type_in_hash_message(relationship)
  I18n.t(
    :invalid_relationship_type_in_hash,
    relationship: relationship.name,
    relationship_type: relationship.type,
    scope: 'fun_with_json_api.schema_validators'
  )
end