Module: Moku6::EnvelopeSchema

Defined in:
lib/moku6/envelope_schema.rb

Overview

Builds the runtime event (envelope) validation JSON Schema from a definition. Shared by JsonSchemaGenerator and ExampleConsistencyRule (see design sections 8 / 12.2).

Constant Summary collapse

TYPE_MAP =
{
  "string" => {"type" => "string"},
  "integer" => {"type" => "integer"},
  "number" => {"type" => "number"},
  "boolean" => {"type" => "boolean"},
  "object" => {"type" => "object"},
  "timestamp" => {"type" => "string", "format" => "date-time"}
}.freeze

Class Method Summary collapse

Class Method Details

.field_type(f) ⇒ Object

: (Hash[String, untyped] f) -> Hash[String, untyped]



49
50
51
52
53
54
# File 'lib/moku6/envelope_schema.rb', line 49

def field_type(f)
  return {"type" => "array", "items" => {}} if f["type"] == "array"

  # dup so each occurrence is an independent object (avoids YAML anchors/aliases).
  TYPE_MAP.fetch(f["type"], {"type" => "string"}).dup
end

.for(event) ⇒ Object

: (Event event) -> Hash[String, untyped]



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/moku6/envelope_schema.rb', line 20

def for(event)
  {
    "$schema" => "https://json-schema.org/draft/2020-12/schema",
    "title" => event.action,
    "type" => "object",
    "required" => %w[event_id action occurred_at actor target metadata],
    "properties" => {
      "action" => {"const" => event.action},
      "event_id" => {"type" => "string"},
      "occurred_at" => {"type" => "string", "format" => "date-time"},
      "actor" => {"type" => "object"},
      "target" => {"type" => "object"},
      "metadata" => (event)
    }
  }
end

.metadata_schema(event) ⇒ Object

The schema for the ‘metadata` object (the definition’s fields). : (Event event) -> Hash[String, untyped]



39
40
41
42
43
44
45
46
# File 'lib/moku6/envelope_schema.rb', line 39

def (event)
  required_meta = event.fields.select { |_, f| f["required"] }.keys
  {
    "type" => "object",
    "required" => required_meta,
    "properties" => event.fields.transform_values { |f| field_type(f) }
  }
end