Class: Schash::Schema::Rule::ArrayOf

Inherits:
Base
  • Object
show all
Defined in:
lib/schash/schema/rule/array_of.rb

Instance Method Summary collapse

Methods inherited from Base

#optional?

Constructor Details

#initialize(rule) ⇒ ArrayOf

Returns a new instance of ArrayOf.



5
6
7
8
9
10
11
# 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

Instance Method Details

#validate(target, position = []) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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