Class: TSJSON::List

Inherits:
Base
  • Object
show all
Defined in:
lib/types/list.rb

Instance Method Summary collapse

Methods inherited from Base

#compile, #index, #property, #valid?

Constructor Details

#initialize(type) ⇒ List

Returns a new instance of List.



3
4
5
6
# File 'lib/types/list.rb', line 3

def initialize(type)
  super()
  @type = type
end

Instance Method Details

#of_typeObject



8
9
10
# File 'lib/types/list.rb', line 8

def of_type
  @type
end

#validate(value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/types/list.rb', line 12

def validate(value)
  unless value.is_a?(::Array)
    raise ScalarValidationError.new(
            expected_type: 'Array',
            received_type: value.class.name,
            received_value: value
          )
  end

  errors = []
  value.each_with_index do |item, index|
    of_type.validate(item)
  rescue ValidationError => e
    errors.push({ index: index, error: e })
  end

  raise ListValidationError.new(errors: errors) if errors.length > 0

  true
end