Class: Objecheck::Validator::EachKeyRule

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

Overview

EachKeyRule validates keys in the target by using each_key

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(validator, key_schema) ⇒ EachKeyRule

Returns a new instance of EachKeyRule.



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

def initialize(validator, key_schema)
  @key_rules = validator.compile_schema(key_schema)
end

Class Method Details

.schemaObject



37
38
39
# File 'lib/objecheck/validator/each_key_rule.rb', line 37

def self.schema
  [{ type: Hash }]
end

Instance Method Details

#validate(target, collector) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/objecheck/validator/each_key_rule.rb', line 24

def validate(target, collector)
  if !target.respond_to?(:each_key)
    collector.add_error('should respond to `each_key`')
    return
  end

  target.each_key do |key|
    collector.add_prefix_in(".key(#{key.inspect})") do
      collector.validate(key, @key_rules)
    end
  end
end