Class: Compel::Validators::ArrayItemsValidator

Inherits:
Base
  • Object
show all
Defined in:
lib/compel/validators/array_validator.rb

Instance Attribute Summary

Attributes inherited from Base

#errors, #input, #output, #schema

Instance Method Summary collapse

Methods inherited from Base

#valid?, validate

Constructor Details

#initialize(input, schema) ⇒ ArrayItemsValidator

Returns a new instance of ArrayItemsValidator.



72
73
74
75
76
77
# File 'lib/compel/validators/array_validator.rb', line 72

def initialize(input, schema)
  super

  @output = []
  @errors = Errors.new
end

Instance Method Details

#validateObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/compel/validators/array_validator.rb', line 79

def validate
  input.each_with_index do |item, index|

    if !schema.required? && item.nil?
      next
    end

    item_validator = \
      if schema.type == Coercion::Hash
        HashValidator.validate(item, schema)
      else
        TypeValidator.validate(item, schema)
      end

    output << item_validator.serialize

    if !item_validator.valid?
      # added errors for the index of that invalid array value
      errors.add("#{index}", item_validator.serialize_errors)
    end
  end

  self
end