Class: Verquest::Properties::Enum
- 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.
Instance Attribute Summary collapse
-
#schema_options ⇒ Object
readonly
private
Returns the value of attribute schema_options.
-
#values ⇒ Object
readonly
private
Returns the value of attribute values.
Attributes inherited from Base
#map, #name, #nullable, #required
Instance Method Summary collapse
-
#initialize(name:, values:, required: false, nullable: false, map: nil, **schema_options) ⇒ Enum
constructor
Initialize a new Enum property.
-
#mapping(key_prefix:, value_prefix:, mapping:, version: nil) ⇒ Hash
Create mapping for this enum property.
-
#to_schema ⇒ Hash
Generate JSON schema definition for this enum.
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
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, **) 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 = &.transform_keys(&:to_s) if nullable && !values.include?("null") values << "null" end end |
Instance Attribute Details
#schema_options ⇒ Object (readonly, private)
Returns the value of attribute schema_options.
62 63 64 |
# File 'lib/verquest/properties/enum.rb', line 62 def end |
#values ⇒ Object (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
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_schema ⇒ Hash
Generate JSON 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() } end |