Class: Restspec::Schema::Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/restspec/schema/checker.rb

Overview

Checks if a response object (a hash, esentially) is valid against a schema.

Defined Under Namespace

Classes: InvalidationError, NoAttributeError, NoObjectError, ObjectChecker

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Checker

Creates a new Restspec::Schema::Checker using a Schema object.


7
8
9
# File 'lib/restspec/schema/checker.rb', line 7

def initialize(schema)
  self.schema = schema
end

Instance Method Details

#check!(object) ⇒ Object

Checks if an object follows the contract provided by the schema. This will just pass through if everything is ok. If something is wrong, an error will be raised. The actual check will be done, attribute by attribute, by an instance of ObjectChecker, calling the methods check_missed_key! and check_invalid!.

Parameters:

  • object (Hash)

    the object to check against the schema.

Raises:

  • NoObjectError if parameter passed is not a hash.


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/restspec/schema/checker.rb', line 25

def check!(object)
  raise NoObjectError.new(object) unless object.is_a?(Hash)

  schema.attributes.each do |_, attribute|
    if attribute.can_be_checked?
      checker = ObjectChecker.new(object, attribute)
      checker.check_missed_key!
      checker.check_invalid!
    end
  end
end

#check_array!(array) ⇒ Object

Checks iteratively through an array of objects.


12
13
14
# File 'lib/restspec/schema/checker.rb', line 12

def check_array!(array)
  array.each { |item| check!(item) }
end