Module: Explicit::Spec::Array

Extended by:
Array
Included in:
Array
Defined in:
lib/explicit/spec/array.rb

Instance Method Summary collapse

Instance Method Details

#build(itemspec, options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/explicit/spec/array.rb', line 6

def build(itemspec, options)
  itemspec_validator = Explicit::Spec.build(itemspec)

  lambda do |values|
    return [:error, :array] if !values.is_a?(::Array)

    if options[:empty] == false && values.empty?
      return [:error, :empty]
    end

    validated = []

    values.each.with_index do |value, index|
      case itemspec_validator.call(value)
      in [:ok, value]  then validated << value
      in [:error, err] then return [:error, [:array, index, err]]
      end
    end

    [:ok, validated]
  end
end