class Objecheck::Validator::AnyRule

AnyRule validates that object meets one of the give rules

Public Class Methods

new(validator, schemas) click to toggle source
# 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
schema() click to toggle source
# File lib/objecheck/validator/any_rule.rb, line 43
def self.schema
  [{ each: { type: Hash } }]
end

Public Instance Methods

validate(target, collector) click to toggle source
# 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