Class: Verquest::Properties::Reference

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

Examples:

Reference another schema

reference = Verquest::Properties::Reference.new(
  name: :user,
  from: UserRequest,
  required: true
)

Reference a specific property from another schema

reference = Verquest::Properties::Reference.new(
  name: :address,
  from: UserRequest,
  property: :address
)

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

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

Parameters:

  • name (String, Symbol)

    The name of the property

  • from (Class)

    The schema class to reference

  • property (Symbol, nil) (defaults to: nil)

    Optional specific property to reference

  • nullable (Boolean) (defaults to: false)

    Whether this property can be null

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

    The mapping path for this property

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

    Whether this property is required, or array of dependency names



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

#fromObject (readonly, private)

Returns the value of attribute from.



108
109
110
# File 'lib/verquest/properties/reference.rb', line 108

def from
  @from
end

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

Parameters:

  • key_prefix (Array<String>)

    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)

    The version to create mapping for

Returns:

  • (Hash)

    The updated mapping hash



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_schemaHash

Generate JSON schema definition for this reference property

Returns:

  • (Hash)

    The schema definition with a $ref pointer



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

Parameters:

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

    The version to generate validation schema for

Returns:

  • (Hash)

    The validation schema for this reference



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