Class: OpenActive::Validators::ArrayOfValidator

Inherits:
BaseValidator show all
Defined in:
lib/openactive/validators/array_of_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseValidator

get_validator

Constructor Details

#initialize(item_validator) ⇒ ArrayOfValidator

Returns a new instance of ArrayOfValidator.



6
7
8
# File 'lib/openactive/validators/array_of_validator.rb', line 6

def initialize(item_validator)
  @item_validator = item_validator
end

Instance Attribute Details

#item_validatorObject

Returns the value of attribute item_validator.



4
5
6
# File 'lib/openactive/validators/array_of_validator.rb', line 4

def item_validator
  @item_validator
end

Instance Method Details

#coerce(value) ⇒ mixed

Coerce given value to the type the validator is validating against. PLEASE NOTE: no checks are performed on the given value. It is therefore recommended to call the “run” method first before this.

Parameters:

  • value (mixed)

    The value to coerce.

Returns:

  • (mixed)

    The same value.



17
18
19
20
# File 'lib/openactive/validators/array_of_validator.rb', line 17

def coerce(value)
  # NOTE: OpenActive is more strict than plain json-ld, so no coercion into arrays
  value
end

#run(value) ⇒ Boolean

Run validation on the given value.

Parameters:

  • value (mixed)

    The value to validate.

Returns:

  • (Boolean)

    Whether validation passes or not.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/openactive/validators/array_of_validator.rb', line 27

def run(value)
  null_validator = NullValidator.new

  # NOTE: OpenActive is more strict than plain json-ld, so no coercion into arrays

  # Check if value is an array
  return true if item_validator.run(value) == true

  return false if ArrayValidator.new.run(value) == false

  value.each do |item|
    # If any of the provided items is not null nor an instance of the provided class name
    return false if null_validator.run(item) == false && item_validator.run(item) == false
  end
  true
end