Class: Verquest::Properties::Reference
- Defined in:
- lib/verquest/properties/reference.rb
Overview
Reference property type for schema reuse
Allows referencing other schema definitions to promote reuse and DRY principles. Can reference either a complete schema or a specific property within a schema.
Instance Attribute Summary collapse
-
#from ⇒ Object
readonly
private
Returns the value of attribute from.
-
#property ⇒ Object
readonly
private
Returns the value of attribute property.
Attributes inherited from Base
#map, #name, #nullable, #required
Instance Method Summary collapse
-
#initialize(name:, from:, property: nil, nullable: false, map: nil, required: false) ⇒ Reference
constructor
Initialize a new Reference property.
-
#mapping(key_prefix:, value_prefix:, mapping:, version:) ⇒ Hash
Create mapping for this reference property This delegates to the referenced schema’s mapping with appropriate key prefixing.
-
#to_schema ⇒ Hash
Generate JSON schema definition for this reference property.
-
#to_validation_schema(version: nil) ⇒ Hash
Generate validation schema for this reference property.
Methods inherited from Base
#add, #mapping_value_key, #mapping_value_prefix
Methods included from HelperMethods::RequiredProperties
#dependent_required_properties, #required_properties
Constructor Details
#initialize(name:, from:, property: nil, nullable: false, map: nil, required: false) ⇒ Reference
Initialize a new Reference property
32 33 34 35 36 37 38 39 |
# File 'lib/verquest/properties/reference.rb', line 32 def initialize(name:, from:, property: nil, nullable: false, map: nil, required: false) @name = name.to_s @from = from @property = property @nullable = nullable @map = map @required = required end |
Instance Attribute Details
#from ⇒ Object (readonly, private)
Returns the value of attribute from.
108 109 110 |
# File 'lib/verquest/properties/reference.rb', line 108 def from @from end |
#property ⇒ Object (readonly, private)
Returns the value of attribute property.
108 109 110 |
# File 'lib/verquest/properties/reference.rb', line 108 def property @property end |
Instance Method Details
#mapping(key_prefix:, value_prefix:, mapping:, version:) ⇒ Hash
Create mapping for this reference property This delegates to the referenced schema’s mapping with appropriate key prefixing
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/verquest/properties/reference.rb', line 85 def mapping(key_prefix:, value_prefix:, mapping:, version:) reference_mapping = from.mapping(version:, property:).dup value_key_prefix = mapping_value_key(value_prefix:) # Single field mapping if property && reference_mapping.size == 1 && !reference_mapping.keys.first.include?("/") reference_mapping = { (key_prefix + [name]).join("/") => value_key_prefix } else if value_key_prefix != "" && !value_key_prefix.end_with?("/") value_key_prefix = "#{value_key_prefix}/" end reference_mapping.transform_keys! { "#{(key_prefix + [name]).join("/")}/#{_1}" } reference_mapping.transform_values! { "#{value_key_prefix}#{_1}" } end mapping.merge!(reference_mapping) end |
#to_schema ⇒ Hash
Generate JSON schema definition for this reference property
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/verquest/properties/reference.rb', line 44 def to_schema if nullable { name => { "oneOf" => [ {"$ref" => from.to_ref(property: property)}, {"type" => "null"} ] } } else { name => {"$ref" => from.to_ref(property: property)} } end end |
#to_validation_schema(version: nil) ⇒ Hash
Generate validation schema for this reference property
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/verquest/properties/reference.rb', line 65 def to_validation_schema(version: nil) schema = from.to_validation_schema(version:, property: property).dup if nullable schema["type"] = [schema["type"], "null"] unless schema["type"].include?("null") end { name => schema } end |