Class: Verquest::Properties::Field

Inherits:
Base
  • Object
show all
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.

Examples:

Define a required string field

field = Verquest::Properties::Field.new(
  name: :email,
  type: :string,
  required: true,
  format: "email"
)

Constant Summary collapse

ALLOWED_TYPES =

List of allowed field types

Returns:

%w[string number integer boolean].freeze

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:, required: false, map: nil, **schema_options) ⇒ Field

Initialize a new Field property

Parameters:

  • name (String, Symbol)

    The name of the property

  • type (String, Symbol)

    The data type for this field, must be one of ALLOWED_TYPES

  • required (Boolean) (defaults to: false)

    Whether this property is required

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

    The mapping path for this property

  • schema_options (Hash)

    Additional JSON schema options for this property

Raises:

  • (ArgumentError)

    If type is not one of the allowed types

  • (ArgumentError)

    If attempting to map a field to root without a name



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, **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 fields to the root without a name" if map == "/"

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

Instance Attribute Details

#schema_optionsObject (readonly, private)

Returns the value of attribute schema_options.



62
63
64
# File 'lib/verquest/properties/field.rb', line 62

def schema_options
  @schema_options
end

#typeObject (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

Parameters:

  • key_prefix (Array<Symbol>)

    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

Returns:

  • (Hash)

    The updated mapping hash



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_schemaHash

Generate JSON schema definition for this field

Returns:

  • (Hash)

    The schema definition for this field



45
46
47
# File 'lib/verquest/properties/field.rb', line 45

def to_schema
  {name => {"type" => type}.merge(schema_options)}
end