class FunWithJsonApi::SchemaValidators::CheckRelationshipNames

Ensures all provided relationship names are known

Attributes

deserializer[R]
document[R]
relationship_keys[R]

Public Class Methods

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

Public Instance Methods

call() click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationship_names.rb, line 19
def call
  unknown = relationship_keys.reject { |rel| resource_relationships.include?(rel) }
  return if unknown.empty?

  unauthorised_relationships = unknown.select do |relationship|
    known_relationships.include?(relationship)
  end
  if unauthorised_relationships.any?
    raise build_unauthorized_relationship_error(unauthorised_relationships)
  else
    raise build_unknown_relationship_error(unknown)
  end
end

Protected Instance Methods

known_relationships() click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationship_names.rb, line 39
def known_relationships
  @known_relationships ||= deserializer.class.relationship_names.map(&:to_s)
end
resource_relationships() click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationship_names.rb, line 35
def resource_relationships
  @resource_relationships ||= deserializer.relationships.map(&:name).map(&:to_s)
end

Private Instance Methods

build_unauthorized_relationship_error(unauthorised_relationships) click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationship_names.rb, line 45
def build_unauthorized_relationship_error(unauthorised_relationships)
  payload = unauthorised_relationships.map do |relationship|
    build_unauthorized_relationship_payload(relationship)
  end
  message = 'Unauthorized relationships were provided by endpoint'
  FunWithJsonApi::Exceptions::UnauthorizedRelationship.new(message, payload)
end
build_unauthorized_relationship_payload(relationship) click to toggle source

Relationship is known, but not supported by this request

# File lib/fun_with_json_api/schema_validators/check_relationship_names.rb, line 62
def build_unauthorized_relationship_payload(relationship)
  ExceptionPayload.new(
    detail: unauthorized_relationship_error(relationship),
    pointer: "/data/relationships/#{relationship}"
  )
end
build_unknown_relationship_error(unknown_relationships) click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationship_names.rb, line 53
def build_unknown_relationship_error(unknown_relationships)
  payload = unknown_relationships.map do |relationship|
    build_unknown_relationship_payload(relationship)
  end
  message = 'Unknown relationships were provided by endpoint'
  FunWithJsonApi::Exceptions::UnknownRelationship.new(message, payload)
end
build_unknown_relationship_payload(relationship) click to toggle source

Relationship is completely unknown, can cannot be assigned to this resource type (ever!)

# File lib/fun_with_json_api/schema_validators/check_relationship_names.rb, line 70
def build_unknown_relationship_payload(relationship)
  ExceptionPayload.new(
    detail: unknown_relationship_error(relationship),
    pointer: "/data/relationships/#{relationship}"
  )
end
unauthorized_relationship_error(relationship) click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationship_names.rb, line 86
def unauthorized_relationship_error(relationship)
  I18n.t(
    :unauthorized_relationship,
    relationship: relationship,
    resource: deserializer.type,
    scope: 'fun_with_json_api.schema_validators'
  )
end
unknown_relationship_error(relationship) click to toggle source
# File lib/fun_with_json_api/schema_validators/check_relationship_names.rb, line 77
def unknown_relationship_error(relationship)
  I18n.t(
    :unknown_relationship_for_resource,
    relationship: relationship,
    resource: deserializer.type,
    scope: 'fun_with_json_api.schema_validators'
  )
end