Class: Objecheck::Validator::EachRule

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

Overview

EachRule validates elements in the target by using each

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(validator, element_schema) ⇒ EachRule



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

def initialize(validator, element_schema)
  @element_rules = validator.compile_schema(element_schema)
end

Class Method Details

.schemaObject



37
38
39
# File 'lib/objecheck/validator/each_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_rule.rb', line 24

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

  target.each.with_index do |elem, i|
    collector.add_prefix_in("[#{i}]") do
      collector.validate(elem, @element_rules)
    end
  end
end