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.
Constant Summary collapse
- ALLOWED_TYPES =
List of allowed 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
Instance Method Summary collapse
-
#initialize(name:, type:, required: 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
Constructor Details
#initialize(name:, type:, required: false, map: nil, **schema_options) ⇒ Field
Initialize a new Field property
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/verquest/properties/field.rb', line 31 def initialize(name:, type:, required: 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 == "/" @name = name.to_s @type = type.to_s @required = required @map = map @schema_options = &.transform_keys(&:to_s) end |
Instance Attribute Details
#schema_options ⇒ Object (readonly, private)
Returns the value of attribute schema_options.
62 63 64 |
# File 'lib/verquest/properties/field.rb', line 62 def @schema_options end |
#type ⇒ Object (readonly, private)
Returns the value of attribute type.
62 63 64 |
# File 'lib/verquest/properties/field.rb', line 62 def type @type end |
Instance Method Details
#mapping(key_prefix:, value_prefix:, mapping:, version: nil) ⇒ Hash
Create mapping for this field property
56 57 58 |
# File 'lib/verquest/properties/field.rb', line 56 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
45 46 47 |
# File 'lib/verquest/properties/field.rb', line 45 def to_schema {name => {"type" => type}.merge()} end |