Class: Objecheck::Validator::EachValueRule

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

Overview

EachValueRule validates values in the target by using each_pair

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(validator, value_schema) ⇒ EachValueRule

Returns a new instance of EachValueRule.



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

def initialize(validator, value_schema)
  @value_rules = validator.compile_schema(value_schema)
end

Class Method Details

.schemaObject



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

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

  target.each_pair do |key, value|
    collector.add_prefix_in("[#{key.inspect}]") do
      collector.validate(value, @value_rules)
    end
  end
end