Class: Verquest::Properties::Enum

Inherits:
Base
  • Object
show all
Defined in:
lib/verquest/properties/enum.rb

Overview

The Enum class represents a enum property with a list of possible values in a JSON schema.

Examples:

enum = Enum.new(name: "type", values: ["member", "admin"])

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

Initialize a new Enum property

Parameters:

  • name (String, Symbol)

    The name of the property

  • values (Array)

    The enum values for this property

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

    Whether this property is required, or array of dependency names

  • 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

Raises:

  • (ArgumentError)

    If attempting to map an enum to root without a name

  • (ArgumentError)

    If values is empty

  • (ArgumentError)

    If values are not unique

  • (ArgumentError)

    If only one value is provided (should use const instead)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/verquest/properties/enum.rb', line 22

def initialize(name:, values:, required: false, nullable: false, map: nil, **schema_options)
  raise ArgumentError, "You can not map enums to the root without a name" if map == "/"
  raise ArgumentError, "Values must not be empty" if values.empty?
  raise ArgumentError, "Values must be unique" if values.uniq.length != values.length
  raise ArgumentError, "Use const for a single value" if values.length == 1

  @name = name.to_s
  @values = values
  @required = required
  @nullable = nullable
  @map = map
  @schema_options = schema_options&.transform_keys(&:to_s)

  if nullable && !values.include?("null")
    values << "null"
  end
end

Instance Attribute Details

#schema_optionsObject (readonly, private)

Returns the value of attribute schema_options.



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

def schema_options
  @schema_options
end

#valuesObject (readonly, private)

Returns the value of attribute values.



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

def values
  @values
end

Instance Method Details

#mapping(key_prefix:, value_prefix:, mapping:, version: nil) ⇒ Hash

Create mapping for this enum 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/enum.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 enum

Returns:

  • (Hash)

    The schema definition for this enum



43
44
45
46
47
# File 'lib/verquest/properties/enum.rb', line 43

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