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

Examples:

Define a required string field

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

Constant Summary collapse

DEFAULT_TYPES =

List of default field types

Returns:

%w[string number integer boolean].freeze

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:, required: false, nullable: 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, can be a default type or a custom field type

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

    Whether this property is required, or array of dependency names (can be overridden by custom type)

  • nullable (Boolean) (defaults to: false)

    Whether this property can be null

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

    The mapping path for this property

  • schema_options (Hash)

    Additional JSON schema options for this property (merged with custom type options)

Raises:

  • (ArgumentError)

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

  • (ArgumentError)

    If attempting to map a field to root without a name



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, **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 == "/"

  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
    @schema_options = if custom_type.key?(:schema_options)
      custom_type[:schema_options].merge(schema_options).transform_keys(&:to_s)
    else
      schema_options.transform_keys(&:to_s)
    end
  else
    @type = type.to_s
    @required = required
    @schema_options = schema_options&.transform_keys(&:to_s)
  end

  @name = name.to_s
  @nullable = nullable
  @map = map

  if nullable
    @type = [@type, "null"]
  end
end

Instance Attribute Details

#schema_optionsObject (readonly, private)

Returns the value of attribute schema_options.



82
83
84
# File 'lib/verquest/properties/field.rb', line 82

def schema_options
  @schema_options
end

#typeObject (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_typesArray<String> (private)

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

Returns:

  • (Array<String>)

    Array of allowed field type names



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

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



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_schemaHash

Generate JSON schema definition for this field

Returns:

  • (Hash)

    The 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(schema_options)
  }
end