Class: Verquest::Properties::Array
- 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.
Instance Attribute Summary collapse
-
#item_schema_options ⇒ Object
readonly
private
Returns the value of attribute item_schema_options.
-
#item_type ⇒ Object
readonly
private
Returns the value of attribute item_type.
-
#schema_options ⇒ Object
readonly
private
Returns the value of attribute schema_options.
-
#type ⇒ Object
readonly
private
Returns the value of attribute type.
Attributes inherited from Base
#map, #name, #nullable, #required
Instance Method Summary collapse
-
#allowed_types ⇒ Array<String>
private
Gets the list of allowed item types, including both default and custom types.
-
#initialize(name:, type:, map: nil, required: false, nullable: false, item_schema_options: {}, **schema_options) ⇒ Array
constructor
Initialize a new Array property.
-
#mapping(key_prefix:, value_prefix:, mapping:, version: nil) ⇒ Hash
Create mapping for this array property.
-
#to_schema ⇒ Hash
Generate JSON schema definition for this array property.
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
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: {}, **) 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().transform_keys(&:to_s) else .transform_keys(&:to_s) end else @item_type = type.to_s @item_schema_options = .transform_keys(&:to_s) end @name = name.to_s @map = map @required = required @nullable = nullable @schema_options = &.transform_keys(&:to_s) @type = if nullable %w[array null] else "array" end end |
Instance Attribute Details
#item_schema_options ⇒ Object (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 end |
#item_type ⇒ Object (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_options ⇒ Object (readonly, private)
Returns the value of attribute schema_options.
83 84 85 |
# File 'lib/verquest/properties/array.rb', line 83 def @schema_options end |
#type ⇒ Object (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_types ⇒ Array<String> (private)
Gets the list of allowed item types, including both default and custom types
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
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_schema ⇒ Hash
Generate JSON 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() }.merge() } end |