Class: RSchema::Schemas::VariableLengthArray

Inherits:
Object
  • Object
show all
Defined in:
lib/rschema/schemas/variable_length_array.rb

Overview

A schema that matches variable-length arrays, where all elements conform to a single subschema

Examples:

A variable-length array schema

schema = RSchema.define { array(_Integer) }
schema.valid?([1,2,3]) #=> true
schema.valid?([]) #=> true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element_schema) ⇒ VariableLengthArray

Returns a new instance of VariableLengthArray.



17
18
19
# File 'lib/rschema/schemas/variable_length_array.rb', line 17

def initialize(element_schema)
  @element_schema = element_schema
end

Instance Attribute Details

#element_schemaObject

Returns the value of attribute element_schema.



15
16
17
# File 'lib/rschema/schemas/variable_length_array.rb', line 15

def element_schema
  @element_schema
end

Instance Method Details

#call(value, options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/rschema/schemas/variable_length_array.rb', line 21

def call(value, options)
  return type_failure(value) unless value.is_a?(Array)

  validated_values, errors = validate_elements(value, options)
  if errors.empty?
    Result.success(validated_values)
  else
    Result.failure(errors)
  end
end

#with_wrapped_subschemas(wrapper) ⇒ Object



32
33
34
# File 'lib/rschema/schemas/variable_length_array.rb', line 32

def with_wrapped_subschemas(wrapper)
  self.class.new(wrapper.wrap(element_schema))
end