class FunWithJsonApi::SchemaValidators::CheckAttributeNames
Attributes
deserializer[R]
document[R]
Public Class Methods
call(document, deserializer)
click to toggle source
# File lib/fun_with_json_api/schema_validators/check_attribute_names.rb, line 4 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_attribute_names.rb, line 11 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_attribute_names.rb, line 16 def call attributes = document['data'].fetch('attributes', {}).keys unknown = attributes.reject { |attribute| resource_attributes.include?(attribute) } check_attribute_names(unknown) if unknown.any? true end
known_attributes()
click to toggle source
# File lib/fun_with_json_api/schema_validators/check_attribute_names.rb, line 29 def known_attributes @known_attributes ||= deserializer.class.attribute_names.map(&:to_s) end
resource_attributes()
click to toggle source
# File lib/fun_with_json_api/schema_validators/check_attribute_names.rb, line 25 def resource_attributes @resource_attributes ||= deserializer.attributes.map(&:name).map(&:to_s) end
Private Instance Methods
build_forbidden_attribute_error(attributes)
click to toggle source
# File lib/fun_with_json_api/schema_validators/check_attribute_names.rb, line 59 def build_forbidden_attribute_error(attributes) payload = attributes.map { |attribute| build_forbidden_attribute_payload(attribute) } message = 'Forbidden attributes were provided by endpoint' FunWithJsonApi::Exceptions::UnauthorizedAttribute.new(message, payload) end
build_forbidden_attribute_payload(attribute)
click to toggle source
# File lib/fun_with_json_api/schema_validators/check_attribute_names.rb, line 65 def build_forbidden_attribute_payload(attribute) ExceptionPayload.new( detail: forbidden_attribute_error(attribute), pointer: "/data/attributes/#{attribute}", status: '403' ) end
build_unknown_attribute_payload(attribute)
click to toggle source
# File lib/fun_with_json_api/schema_validators/check_attribute_names.rb, line 52 def build_unknown_attribute_payload(attribute) ExceptionPayload.new( detail: unknown_attribute_error(attribute), pointer: "/data/attributes/#{attribute}" ) end
build_unknown_attributes_error(attributes)
click to toggle source
# File lib/fun_with_json_api/schema_validators/check_attribute_names.rb, line 46 def build_unknown_attributes_error(attributes) payload = attributes.map { |attribute| build_unknown_attribute_payload(attribute) } message = 'Unknown attributes were provided by endpoint' FunWithJsonApi::Exceptions::UnknownAttribute.new(message, payload) end
check_attribute_names(unknown)
click to toggle source
# File lib/fun_with_json_api/schema_validators/check_attribute_names.rb, line 35 def check_attribute_names(unknown) unauthorised_attributes = unknown.select do |attribute| known_attributes.include?(attribute) end if unauthorised_attributes.any? raise build_forbidden_attribute_error(unauthorised_attributes) else raise build_unknown_attributes_error(unknown) end end
forbidden_attribute_error(attribute)
click to toggle source
# File lib/fun_with_json_api/schema_validators/check_attribute_names.rb, line 82 def forbidden_attribute_error(attribute) I18n.t( :forbidden_attribute_for_request, attribute: attribute, resource: deserializer.type, scope: 'fun_with_json_api.schema_validators' ) end
unknown_attribute_error(attribute)
click to toggle source
# File lib/fun_with_json_api/schema_validators/check_attribute_names.rb, line 73 def unknown_attribute_error(attribute) I18n.t( :unknown_attribute_for_resource, attribute: attribute, resource: deserializer.type, scope: 'fun_with_json_api.schema_validators' ) end