Class: FunWithJsonApi::SchemaValidator

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, deserializer, resource) ⇒ SchemaValidator

Returns a new instance of SchemaValidator.



15
16
17
18
19
# File 'lib/fun_with_json_api/schema_validator.rb', line 15

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

Instance Attribute Details

#deserializerObject (readonly)

Returns the value of attribute deserializer.



12
13
14
# File 'lib/fun_with_json_api/schema_validator.rb', line 12

def deserializer
  @deserializer
end

#documentObject (readonly)

Returns the value of attribute document.



11
12
13
# File 'lib/fun_with_json_api/schema_validator.rb', line 11

def document
  @document
end

#resourceObject (readonly)

Returns the value of attribute resource.



13
14
15
# File 'lib/fun_with_json_api/schema_validator.rb', line 13

def resource
  @resource
end

Class Method Details

.check(document, deserializer, resource) ⇒ Object



5
6
7
# File 'lib/fun_with_json_api/schema_validator.rb', line 5

def self.check(document, deserializer, resource)
  new(document, deserializer, resource).check
end

Instance Method Details

#checkObject



21
22
23
24
25
26
# File 'lib/fun_with_json_api/schema_validator.rb', line 21

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

#document_idObject



28
29
30
# File 'lib/fun_with_json_api/schema_validator.rb', line 28

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

#document_typeObject



32
33
34
# File 'lib/fun_with_json_api/schema_validator.rb', line 32

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

#resource_idObject



36
37
38
# File 'lib/fun_with_json_api/schema_validator.rb', line 36

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

#resource_typeObject



40
41
42
# File 'lib/fun_with_json_api/schema_validator.rb', line 40

def resource_type
  @resource_type ||= deserializer.type
end