Class: KubeDSL::Validations::ArrayValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/kube-dsl/validations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field_name, opts) ⇒ ArrayValidator

Returns a new instance of ArrayValidator.



50
51
52
53
# File 'lib/kube-dsl/validations.rb', line 50

def initialize(field_name, opts)
  @field_name = field_name
  @kind_of = opts.fetch(:kind_of)
end

Instance Attribute Details

#field_nameObject (readonly)

Returns the value of attribute field_name.



48
49
50
# File 'lib/kube-dsl/validations.rb', line 48

def field_name
  @field_name
end

#kind_ofObject (readonly)

Returns the value of attribute kind_of.



48
49
50
# File 'lib/kube-dsl/validations.rb', line 48

def kind_of
  @kind_of
end

Instance Method Details

#validate(obj, errors, nesting) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/kube-dsl/validations.rb', line 55

def validate(obj, errors, nesting)
  unless obj.is_a?(Array)
    errors.add([*nesting, field_name].join('.'), 'is not an array')
    return
  end

  obj.each_with_index do |elem, idx|
    unless elem.nil? || elem.is_a?(kind_of)
      errors.add(
        [*nesting, field_name].join('.'), "contains an object at index #{idx} "\
          "of type '#{elem.class.name}', expected '#{kind_of.name}'"
      )
    end
  end
end