Class: Compel::Validators::ArrayValidator

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

Instance Attribute Summary collapse

Attributes inherited from Base

#errors, #input, #output, #schema

Instance Method Summary collapse

Methods inherited from Base

#valid?, validate

Constructor Details

#initialize(input, schema) ⇒ ArrayValidator

Returns a new instance of ArrayValidator.



8
9
10
11
12
13
14
# File 'lib/compel/validators/array_validator.rb', line 8

def initialize(input, schema)
  super

  @errors = Errors.new
  @output = []
  @items_schema = schema.options[:items][:value] if schema.options[:items]
end

Instance Attribute Details

#items_schemaObject (readonly)

Returns the value of attribute items_schema.



6
7
8
# File 'lib/compel/validators/array_validator.rb', line 6

def items_schema
  @items_schema
end

Instance Method Details

#serialize_errorsObject



38
39
40
# File 'lib/compel/validators/array_validator.rb', line 38

def serialize_errors
  @errors.to_hash
end

#validateObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/compel/validators/array_validator.rb', line 16

def validate
  unless array_valid?
    return self
  end

  if items_schema.nil?
    @output = input
    return self
  end

  items_validator = \
    ArrayItemsValidator.validate(input, items_schema)

  @output = items_validator.output

  unless items_validator.valid?
    @errors = items_validator.errors
  end

  self
end