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). Supports both default item types and custom field types defined in the configuration.

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, #nullable, #required

Instance Method Summary collapse

Methods inherited from Base

#add, #mapping_value_key, #mapping_value_prefix, #to_validation_schema

Methods included from HelperMethods::RequiredProperties

#dependent_required_properties, #required_properties

Constructor Details

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

Initialize a new Array property

Parameters:

  • name (String, Symbol)

    The name of the property

  • type (String, Symbol)

    The type of items in the array, can be a default type or a custom field type

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

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

  • required (Boolean, Array<Symbol>) (defaults to: false)

    Whether this property is required, or array of dependency names

  • nullable (Boolean) (defaults to: false)

    Whether this property can be null

  • item_schema_options (Hash) (defaults to: {})

    Additional JSON schema options for the array items (merged with custom type options)

  • schema_options (Hash)

    Additional JSON schema options for the array property itself

Raises:

  • (ArgumentError)

    If type is not one of the allowed types (default or custom)

  • (ArgumentError)

    If attempting to map an array to the root



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/verquest/properties/array.rb', line 29

def initialize(name:, type:, map: nil, required: false, nullable: false, item_schema_options: {}, **schema_options)
  raise ArgumentError, "Type must be one of #{allowed_types.join(", ")}" unless allowed_types.include?(type.to_s)
  raise ArgumentError, "You can not map array to the root" if map == "/"

  if (custom_type = Verquest.configuration.custom_field_types[type.to_sym])
    @item_type = custom_type[:type].to_s
    @item_schema_options = if custom_type.key?(:schema_options)
      custom_type[:schema_options].merge(item_schema_options).transform_keys(&:to_s)
    else
      item_schema_options.transform_keys(&:to_s)
    end
  else
    @item_type = type.to_s
    @item_schema_options = item_schema_options.transform_keys(&:to_s)
  end

  @name = name.to_s
  @map = map
  @required = required
  @nullable = nullable
  @schema_options = schema_options&.transform_keys(&:to_s)

  @type = if nullable
    %w[array null]
  else
    "array"
  end
end

Instance Attribute Details

#item_schema_optionsObject (readonly, private)

Returns the value of attribute item_schema_options.



83
84
85
# File 'lib/verquest/properties/array.rb', line 83

def item_schema_options
  @item_schema_options
end

#item_typeObject (readonly, private)

Returns the value of attribute item_type.



83
84
85
# File 'lib/verquest/properties/array.rb', line 83

def item_type
  @item_type
end

#schema_optionsObject (readonly, private)

Returns the value of attribute schema_options.



83
84
85
# File 'lib/verquest/properties/array.rb', line 83

def schema_options
  @schema_options
end

#typeObject (readonly, private)

Returns the value of attribute type.



83
84
85
# File 'lib/verquest/properties/array.rb', line 83

def type
  @type
end

Instance Method Details

#allowed_typesArray<String> (private)

Gets the list of allowed item types, including both default and custom types

Returns:

  • (Array<String>)

    Array of allowed item type names



88
89
90
# File 'lib/verquest/properties/array.rb', line 88

def allowed_types
  Verquest::Properties::Field::DEFAULT_TYPES + Verquest.configuration.custom_field_types.keys.map(&:to_s)
end

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

Create mapping for this array property

Parameters:

  • key_prefix (Array<String>)

    Prefix for the source key

  • value_prefix (Array<String>)

    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



77
78
79
# File 'lib/verquest/properties/array.rb', line 77

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



61
62
63
64
65
66
67
68
# File 'lib/verquest/properties/array.rb', line 61

def to_schema
  {
    name => {
      "type" => type,
      "items" => {"type" => item_type}.merge(item_schema_options)
    }.merge(schema_options)
  }
end