File: array_of.rb

package info (click to toggle)
ruby-schash 0.1.2%2Bgh-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 164 kB
  • sloc: ruby: 325; sh: 3; makefile: 3
file content (31 lines) | stat: -rw-r--r-- 656 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module Schash
  module Schema
    module Rule
      class ArrayOf < Base
        def initialize(rule)
          @rule = if rule.is_a?(::Hash)
                    Rule::Hash.new(rule)
                  else
                    rule
                  end
        end

        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
      end
    end
  end
end