Class: Verquest::Properties::Field
- Defined in:
- lib/verquest/properties/field.rb
Overview
Field property type for basic scalar values
Represents simple scalar types (string, number, integer, boolean) in the schema. Used for defining basic data fields without nesting. Supports both default types and custom field types defined in the configuration.
Constant Summary collapse
- DEFAULT_TYPES =
List of default field types
%w[string number integer boolean].freeze
Instance Attribute Summary collapse
-
#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 field types, including both default and custom types.
-
#initialize(name:, type:, required: false, nullable: false, map: nil, **schema_options) ⇒ Field
constructor
Initialize a new Field property.
-
#mapping(key_prefix:, value_prefix:, mapping:, version: nil) ⇒ Hash
Create mapping for this field property.
-
#to_schema ⇒ Hash
Generate JSON schema definition for this field.
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:, required: false, nullable: false, map: nil, **schema_options) ⇒ Field
Initialize a new Field property
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/verquest/properties/field.rb', line 33 def initialize(name:, type:, required: false, nullable: false, map: nil, **) raise ArgumentError, "Type must be one of #{allowed_types.join(", ")}" unless allowed_types.include?(type.to_s) raise ArgumentError, "You can not map fields to the root without a name" if map == "/" if (custom_type = Verquest.configuration.custom_field_types[type.to_sym]) @type = custom_type[:type].to_s @required = custom_type.key?(:required) ? custom_type[:required] : required = if custom_type.key?(:schema_options) custom_type[:schema_options].merge().transform_keys(&:to_s) else .transform_keys(&:to_s) end else @type = type.to_s @required = required = &.transform_keys(&:to_s) end @name = name.to_s @nullable = nullable @map = map if nullable @type = [@type, "null"] end end |
Instance Attribute Details
#schema_options ⇒ Object (readonly, private)
Returns the value of attribute schema_options.
82 83 84 |
# File 'lib/verquest/properties/field.rb', line 82 def end |
#type ⇒ Object (readonly, private)
Returns the value of attribute type.
82 83 84 |
# File 'lib/verquest/properties/field.rb', line 82 def type @type end |
Instance Method Details
#allowed_types ⇒ Array<String> (private)
Gets the list of allowed field types, including both default and custom types
87 88 89 |
# File 'lib/verquest/properties/field.rb', line 87 def allowed_types 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 field property
76 77 78 |
# File 'lib/verquest/properties/field.rb', line 76 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 field
63 64 65 66 67 |
# File 'lib/verquest/properties/field.rb', line 63 def to_schema { name => {"type" => type}.merge() } end |