Class: Schemacop::V2::ArrayValidator

Inherits:
NodeSupportingType show all
Defined in:
lib/schemacop/v2/validator/array_validator.rb

Instance Attribute Summary

Attributes inherited from Node

#options

Instance Method Summary collapse

Methods inherited from NodeSupportingType

build, #exec_block, #type

Methods inherited from NodeWithBlock

block_method, #exec_block

Methods inherited from Node

build, class_matches?, clear_klasses, clear_symbols, #exec_block, klass, option, #option, #option?, register, #resolve_type_klass, symbol, symbol_matches?, #type_filter_matches?, #type_label, type_matches?, #type_matches?

Constructor Details

#initialize(*args) ⇒ ArrayValidator

Returns a new instance of ArrayValidator.



10
11
12
13
# File 'lib/schemacop/v2/validator/array_validator.rb', line 10

def initialize(*args)
  super
  type(:nil) if option(:nil)
end

Instance Method Details

#validate(data, collector) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/schemacop/v2/validator/array_validator.rb', line 15

def validate(data, collector)
  validate_custom_check(data, collector)

  if option?(:min) && data.size < option(:min)
    collector.error "Array must have more (>=) than #{option(:min)} elements."
  end
  if option?(:max) && data.size > option(:max)
    collector.error "Array must have less (<=) than #{option(:max)} elements."
  end
  data.each_with_index do |entry, index|
    collector.path("[#{index}]", index, :array) do
      validate_types(entry, collector)
    end
  end
end