class Schash::Schema::Rule::ArrayOf

Public Class Methods

new(rule) click to toggle source
# File lib/schash/schema/rule/array_of.rb, line 5
def initialize(rule)
  @rule = if rule.is_a?(::Hash)
            Rule::Hash.new(rule)
          else
            rule
          end
end

Public Instance Methods

validate(target, position = []) click to toggle source
# File lib/schash/schema/rule/array_of.rb, line 13
def validate(target, position = [])
  errors = []

  unless target.is_a?(Array)
    errors << Error.new(position, "is not an array")
    return errors
  end

  errors += target.each_with_index.map do |t, i|
    @rule.validate(t, position + [i])
  end.flatten

  errors
end