Class: OpenActive::Validators::ArrayOfValidator
- Inherits:
-
BaseValidator
- Object
- BaseValidator
- OpenActive::Validators::ArrayOfValidator
- Defined in:
- lib/openactive/validators/array_of_validator.rb
Instance Attribute Summary collapse
-
#item_validator ⇒ Object
Returns the value of attribute item_validator.
Instance Method Summary collapse
-
#coerce(value) ⇒ mixed
Coerce given value to the type the validator is validating against.
-
#initialize(item_validator) ⇒ ArrayOfValidator
constructor
A new instance of ArrayOfValidator.
-
#run(value) ⇒ Boolean
Run validation on the given value.
Methods inherited from BaseValidator
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_validator ⇒ Object
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.
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.
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 |