Class: Servus::Schema::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/servus/schema/compiler.rb

Overview

Resolves $ref pointers in a schema against the Servus::Schema registry, producing a self-contained schema with no refs left in it.

One instance per compile. The instance carries the cycle-detection state and the context label used in error messages; the memo it consults is process-wide and lives on Servus::Schema.

Sibling properties

Keys alongside a $ref override the resolved target:

{ '$ref' => '#/core/$defs/amount', 'description' => 'Fee charged' }

This is a template-and-override reading, which is what makes shared fragments usable in practice — you take the shape and re-describe it for the site that uses it. Note that it differs from JSON Schema 2019-09 and later, where properties beside a $ref are an additional subschema applied as an intersection rather than an override.

Siblings are compiled independently and merged onto an already-resolved target, rather than merged first and resolved after. That ordering is what makes the target cacheable: the memo holds a value that does not depend on the call site.

See Also:

Constant Summary collapse

MAX_DEPTH =

Maximum structural nesting depth before DepthExceededError is raised.

This is a runaway guard for pathological input, not a cycle check — cycles are caught exactly by #resolve_ref's visited set, however deep or shallow they are. Keeping the two separate means a legitimately deep acyclic schema compiles instead of being misreported as circular.

100
METADATA_KEYS =

Keys stripped from a fragment when it is spliced into another schema.

json-schema resolves a nested $schema against its registered validators and raises JSON::Schema::SchemaError when it does not recognize the URI — at any position in the document, not just the root. Fragments authored as standalone documents routinely carry these, so they are dropped on splice rather than left to blow up at validation time.

%w[$schema $id id].freeze

Instance Method Summary collapse

Constructor Details

#initialize(context: nil) ⇒ Compiler

Returns a new instance of Compiler.

Parameters:

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

    label for the schema being compiled, used in error messages, e.g. "Treasury::TransferGold::Service arguments schema"



53
54
55
56
# File 'lib/servus/schema/compiler.rb', line 53

def initialize(context: nil)
  @context = context
  @path = []
end

Instance Method Details

#compile(schema) ⇒ Object

Compiles a schema, replacing every $ref with the fragment it names.

Parameters:

  • schema (Object)

    the authored schema

Returns:

  • (Object)

    the compiled schema

Raises:

  • (Error)

    if any ref cannot be resolved



63
64
65
# File 'lib/servus/schema/compiler.rb', line 63

def compile(schema)
  resolve(schema, 0)
end