Class: Objecheck::Validator::TypeRule

Inherits:
Object
  • Object
show all
Defined in:
lib/objecheck/validator/type_rule.rb

Overview

TypeRule validates type of a target

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_validator, type) ⇒ TypeRule

Returns a new instance of TypeRule.



20
21
22
# File 'lib/objecheck/validator/type_rule.rb', line 20

def initialize(_validator, type)
  @type = type
end

Class Method Details

.schemaObject



32
33
34
# File 'lib/objecheck/validator/type_rule.rb', line 32

def self.schema
  [{ any: [{ type: Module }, { eq: :bool }] }]
end

Instance Method Details

#validate(target, collector) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/objecheck/validator/type_rule.rb', line 24

def validate(target, collector)
  if @type == :bool
    collector.add_error("should be a bool (got #{target.class})") if target != true && target != false
  elsif !target.is_a?(@type)
    collector.add_error("should be a #{@type} (got #{target.class})")
  end
end