Class: Objecheck::Validator::AnyRule
- Inherits:
-
Object
- Object
- Objecheck::Validator::AnyRule
- Defined in:
- lib/objecheck/validator/any_rule.rb
Overview
AnyRule validates that object meets one of the give rules
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(validator, schemas) ⇒ AnyRule
constructor
A new instance of AnyRule.
- #validate(target, collector) ⇒ Object
Constructor Details
#initialize(validator, schemas) ⇒ AnyRule
Returns a new instance of AnyRule.
20 21 22 23 24 |
# File 'lib/objecheck/validator/any_rule.rb', line 20 def initialize(validator, schemas) @child_rules = schemas.map do |schema| validator.compile_schema(schema) end end |
Class Method Details
.schema ⇒ Object
43 44 45 |
# File 'lib/objecheck/validator/any_rule.rb', line 43 def self.schema [{ each: { type: Hash } }] end |
Instance Method Details
#validate(target, collector) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/objecheck/validator/any_rule.rb', line 26 def validate(target, collector) t = collector.transaction t.add_error("should satisfy one of the #{@child_rules.size} schemas") t_for_nested_rules = t.transaction result = @child_rules.each.with_index(1).any? do |rules, i| t_for_nested_rules.add_prefix_in("(option #{i})") { t_for_nested_rules.validate(target, rules) } end if result t.rollback(t_for_nested_rules) collector.rollback(t) else t.commit(t_for_nested_rules) collector.commit(t) end end |