class Restspec::Schema::Checker
Checks if a response object (a hash, esentially) is valid against a schema.
Attributes
schema[RW]
Public Class Methods
new(schema)
click to toggle source
Creates a new {Checker} using a {Schema} object.
# File lib/restspec/schema/checker.rb, line 7 def initialize(schema) self.schema = schema end
Public Instance Methods
check!(object)
click to toggle source
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 {ObjectChecker#check_missed_key! check_missed_key!} and {ObjectChecker#check_invalid! check_invalid!}.
@param object [Hash] the object to check against the schema. @raise NoObjectError
if parameter passed is not a hash.
# 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)
click to toggle source
Checks iteratively through an array of objects.
# File lib/restspec/schema/checker.rb, line 12 def check_array!(array) array.each { |item| check!(item) } end