Sha256: a2ec15b93e459d1376bd10b6f150578698b291e7ddfcfd6321b6b0e060bdecce

Contents?: true

Size: 1.28 KB

Versions: 8

Compression:

Stored size: 1.28 KB

Contents

require 'fun_with_json_api/exception'

module FunWithJsonApi
  class SchemaValidator
    def self.check(document, deserializer, resource)
      new(document, deserializer, resource).check
    end

    private_class_method :new

    attr_reader :document
    attr_reader :deserializer
    attr_reader :resource

    def initialize(document, deserializer, resource)
      @document = FunWithJsonApi.sanitize_document(document)
      @deserializer = deserializer
      @resource = resource
    end

    def check
      FunWithJsonApi::SchemaValidators::CheckDocumentTypeMatchesResource.call(self)
      FunWithJsonApi::SchemaValidators::CheckDocumentIdMatchesResource.call(self)
      FunWithJsonApi::SchemaValidators::CheckAttributeNames.call(document, deserializer)
      FunWithJsonApi::SchemaValidators::CheckRelationships.call(document, deserializer)
    end

    def document_id
      @document_id ||= document['data']['id']
    end

    def document_type
      @document_type ||= document['data']['type']
    end

    def resource_id
      @resource_id ||= resource.send(deserializer.id_param).to_s
    end

    def resource_type
      @resource_type ||= deserializer.type
    end
  end
end

# Load known Schema Validators
Dir["#{File.dirname(__FILE__)}/schema_validators/**/*.rb"].each { |f| require f }

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
fun_with_json_api-0.0.14 lib/fun_with_json_api/schema_validator.rb
fun_with_json_api-0.0.13 lib/fun_with_json_api/schema_validator.rb
fun_with_json_api-0.0.11.3 lib/fun_with_json_api/schema_validator.rb
fun_with_json_api-0.0.11.2 lib/fun_with_json_api/schema_validator.rb
fun_with_json_api-0.0.11.1 lib/fun_with_json_api/schema_validator.rb
fun_with_json_api-0.0.11 lib/fun_with_json_api/schema_validator.rb
fun_with_json_api-0.0.10.4 lib/fun_with_json_api/schema_validator.rb
fun_with_json_api-0.0.10.3 lib/fun_with_json_api/schema_validator.rb