Class: Restspec::Schema::Checker
- Inherits:
-
Object
- Object
- Restspec::Schema::Checker
- 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, NoRootFoundError, ObjectChecker
Instance Method Summary collapse
-
#check!(object) ⇒ Object
Checks if an object follows the contract provided by the schema.
-
#check_array!(array) ⇒ Object
Checks iteratively through an array of objects.
- #initialize(schema) ⇒ Checker constructor
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!.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/restspec/schema/checker.rb', line 25 def check!(object) raise NoObjectError.new(object) unless object.is_a?(Hash) raise NoRootFoundError.new(object, schema) if schema.root? && !object.has_key?(schema.root_name) if schema.root? object = object.fetch(schema.root_name) end 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 |