Class: Verquest::Properties::Array

Inherits:
Base
  • Object
show all
Defined in:
lib/verquest/properties/array.rb

Overview

Array property type for schema generation and mapping

Represents an array data structure in the schema with specified item type. Used to define arrays of scalar types (string, number, integer, boolean).

Examples:

Define an array of strings

array = Verquest::Properties::Array.new(
  name: :tags,
  type: :string,
  required: true
)

Instance Attribute Summary collapse

Attributes inherited from Base

#map, #name, #required

Instance Method Summary collapse

Methods inherited from Base

#add, #mapping_value_key, #mapping_value_prefix, #to_validation_schema

Constructor Details

#initialize(name:, type:, map: nil, required: false, **schema_options) ⇒ Array

Initialize a new Array property

Parameters:

  • name (Symbol)

    The name of the property

  • type (Symbol)

    The type of items in the array

  • map (String, nil) (defaults to: nil)

    The mapping path for this property (nil for no explicit mapping)

  • required (Boolean) (defaults to: false)

    Whether this property is required

  • schema_options (Hash)

    Additional JSON schema options for this property

Raises:

  • (ArgumentError)

    If attempting to map an array to the root



25
26
27
28
29
30
31
32
33
# File 'lib/verquest/properties/array.rb', line 25

def initialize(name:, type:, map: nil, required: false, **schema_options)
  raise ArgumentError, "You can not map array to the root" if map == "/"

  @name = name
  @type = type
  @map = map
  @required = required
  @schema_options = schema_options
end

Instance Attribute Details

#schema_optionsObject (readonly, private)

Returns the value of attribute schema_options.



60
61
62
# File 'lib/verquest/properties/array.rb', line 60

def schema_options
  @schema_options
end

#typeObject (readonly, private)

Returns the value of attribute type.



60
61
62
# File 'lib/verquest/properties/array.rb', line 60

def type
  @type
end

Instance Method Details

#mapping(key_prefix:, value_prefix:, mapping:, version: nil) ⇒ Hash

Create mapping for this array property

Parameters:

  • key_prefix (Array<Symbol>)

    Prefix for the source key

  • value_prefix (Array<Symbol>)

    Prefix for the target value

  • mapping (Hash)

    The mapping hash to be updated

  • version (String, nil) (defaults to: nil)

    The version to create mapping for, defaults to configuration setting

Returns:

  • (Hash)

    The updated mapping hash



54
55
56
# File 'lib/verquest/properties/array.rb', line 54

def mapping(key_prefix:, value_prefix:, mapping:, version: nil)
  mapping[(key_prefix + [name]).join(".")] = mapping_value_key(value_prefix:)
end

#to_schemaHash

Generate JSON schema definition for this array property

Returns:

  • (Hash)

    The schema definition for this array property



38
39
40
41
42
43
44
45
# File 'lib/verquest/properties/array.rb', line 38

def to_schema
  {
    name => {
      type: :array,
      items: {type: type}
    }.merge(schema_options)
  }
end