Module: SchemaGraphy::SchemaUtils

Defined in:
lib/schemagraphy/schema_utils.rb

Overview

A utility module for introspecting schema definitions. Provides methods for retrieving metadata, default values, and type information from a schema hash using a dot-separated path syntax.

Class Method Summary collapse

Class Method Details

.crawl_meta(schema, path = nil) ⇒ Hash

Crawl the schema to find the metadata for a given path.

Parameters:

  • schema (Hash)
    The schema hash.
  • path (String, nil) (defaults to: nil)
    The dot-separated path.

Returns:

  • (Hash)
    The metadata hash.


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/schemagraphy/schema_utils.rb', line 108

def self.crawl_meta schema, path = nil
  parts = path ? path.split('.') : []
  node = schema['$schema'] || schema
  meta = {}

  parts.each do |part|
    node = node['properties'][part] if node['properties']&.key?(part)
    break unless node.is_a?(Hash)

    # Only update meta if this level has it
    meta = node if node['templating']
  end

  meta['$meta'] || meta['sgyml'] || meta['templating'] || {}
end

.crawl_properties(schema, path) ⇒ Hash?

Retrieve a nested property definition from a schema using a dot-separated path.

Examples:

Schema Structure
schema = {
  "$schema": {
    "properties": {
      "property1": {
        "properties": {
          "subproperty1": {
            "default": "value1",
            "type": "String"
          }
        }
      }
    }
  }
}
crawl_properties(schema, "property1.subproperty1")
# => { "default" => "value1", "type" => "String" }

Parameters:

  • schema (Hash)
    The schema hash to crawl.
  • path (String)
    The dot-separated path to the property.

Returns:

  • (Hash, nil)
    The property definition hash, or `nil` if not found.


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/schemagraphy/schema_utils.rb', line 33

def crawl_properties schema, path
  path_components = path.split('.')
  current = schema['$schema'] || schema

  path_components.each do |component|
    return nil unless current.is_a?(Hash)
    return nil unless current['properties']&.key?(component)

    current = current['properties'][component]
  end

  current
end

.default_for(schema, path) ⇒ Object?

Get the default value for a property from the schema.

Parameters:

  • schema (Hash)
    The schema hash.
  • path (String)
    The dot-separated path to the property.

Returns:

  • (Object, nil)
    The default value, or `nil` if not defined.


52
53
54
55
56
57
# File 'lib/schemagraphy/schema_utils.rb', line 52

def default_for schema, path
  property = crawl_properties(schema, path)
  return nil unless property.is_a?(Hash)

  property['default'] || property['dflt']
end

.templated_field?(schema, path) ⇒ Boolean

Check if a property is a templated field.

Parameters:

  • schema (Hash)
    The schema hash.
  • path (String)
    The dot-separated path to the property.

Returns:

  • (Boolean)
    `true` if the field has templating configured, `false` otherwise.


96
97
98
99
100
101
# File 'lib/schemagraphy/schema_utils.rb', line 96

def templated_field? schema, path
  property = crawl_properties(schema, path)
  return false unless property.is_a?(Hash)

  property.key?('templating') && property['templating'].is_a?(Hash)
end

.templating_config_for(schema, path) ⇒ Hash

Get the templating configuration for a property from the schema.

Parameters:

  • schema (Hash)
    The schema hash.
  • path (String)
    The dot-separated path to the property.

Returns:

  • (Hash)
    The templating configuration hash.


76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/schemagraphy/schema_utils.rb', line 76

def templating_config_for schema, path
  property = crawl_properties(schema, path)
  return {} unless property.is_a?(Hash)

  return property['templating'] if property['templating']

  if property['type'].to_s.downcase == 'liquid'
    { 'default' => 'liquid', 'delay' => true }
  elsif property['type'].to_s.downcase == 'erb'
    { 'default' => 'erb', 'delay' => true }
  else
    {}
  end
end

.type_for(schema, path) ⇒ String?

Get the type for a property from the schema.

Parameters:

  • schema (Hash)
    The schema hash.
  • path (String)
    The dot-separated path to the property.

Returns:

  • (String, nil)
    The property type, or `nil` if not defined.


64
65
66
67
68
69
# File 'lib/schemagraphy/schema_utils.rb', line 64

def type_for schema, path
  property = crawl_properties(schema, path)
  return nil unless property.is_a?(Hash)

  property['type']
end