Class: TSJSON::List
Instance Method Summary collapse
-
#initialize(type) ⇒ List
constructor
A new instance of List.
- #of_type ⇒ Object
- #validate(value) ⇒ Object
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_type ⇒ Object
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 |