class Restspec::Schema::Checker::ObjectChecker

Checks an object against a schema's attribute definition.

Attributes

attribute[RW]
object[RW]

Public Class Methods

new(object, attribute) click to toggle source
# File lib/restspec/schema/checker.rb, line 49
def initialize(object, attribute)
  self.object = object
  self.attribute = attribute
end

Public Instance Methods

check_invalid!() click to toggle source

Calls {#invalid?} and if the call is true, raises a {InvalidationError}.

# File lib/restspec/schema/checker.rb, line 101
def check_invalid!
  raise InvalidationError.new(object, attribute) if invalid?
end
check_missed_key!() click to toggle source

Calls {#missed_key?} and if the call is true, raises a {NoAttributeError}.

# File lib/restspec/schema/checker.rb, line 74
def check_missed_key!
  raise NoAttributeError.new(object, attribute) if missed_key?
end
invalid?() click to toggle source

Checks if the attribute's type validation fails with the object' attribute. To do this, the valid? method of the type is executed.

@example

# Given the following schema
schema :product do
  attribute :name, string
end

ObjectChecker.new({ name: 10 }, schema.attributes[:name]).invalid?
# true
ObjectChecker.new({ name: 'John' }, schema.attributes[:name]).invalid?
# false

@return [true, false] If the attribute's type validation fails

with the object' attribute.
# File lib/restspec/schema/checker.rb, line 95
def invalid?
  !attribute.type.totally_valid?(attribute, object.fetch(attribute.name))
end
missed_key?() click to toggle source

Checks if the attribute's key is absent from the object.

@example

# Given the following schema
schema :product do
  attribute :name, string
end

ObjectChecker.new({ age: 10 }, schema.attributes[:name]).missed_key?
# true
ObjectChecker.new({ name: 'John' }, schema.attributes[:name]).missed_key?
# false

@return [true, false] If the attribute's key is absent from the object

# File lib/restspec/schema/checker.rb, line 68
def missed_key?
  !object.has_key?(attribute.name)
end