Module: Async::Matrix::Schema

Defined in:
lib/async/matrix/schema.rb,
lib/async/matrix/schema/registry.rb,
lib/async/matrix/schema/validation_error.rb

Overview

Schema-driven validation for Matrix events using the official matrix-org/matrix-spec YAML schemas and the json_schemer gem.

Schemas are loaded lazily from data/matrix-spec/event-schemas/schema/ and cached for the process lifetime. This ensures validation is always up-to-date with the spec – just re-run ‘bin/fetch-matrix-schemas` to pull the latest.

# Look up a schema by event type
Async::Matrix::Schema["m.room.message"]  # => JSONSchemer::Schema

# Validate an event hash
Async::Matrix::Schema.valid?(event_hash)  # => true/false
Async::Matrix::Schema.validate(event_hash) # => [errors]

# List all known event types
Async::Matrix::Schema.event_types  # => ["m.accepted_terms", "m.call.answer", ...]

Defined Under Namespace

Classes: Registry, ValidationError

Class Method Summary collapse

Class Method Details

.[](event_type) ⇒ JSONSchemer::Schema?

Look up a schema by Matrix event type.

Parameters:

  • event_type (String)

    e.g. “m.room.message”

Returns:

  • (JSONSchemer::Schema, nil)


33
34
35
# File 'lib/async/matrix/schema.rb', line 33

def [](event_type)
	Registry.instance[event_type]
end

.content_properties(event_type) ⇒ Array<String>

Content properties defined by the schema for a given event type.

Returns:

  • (Array<String>)


73
74
75
# File 'lib/async/matrix/schema.rb', line 73

def content_properties(event_type)
	Registry.instance.content_properties(event_type)
end

.event_typesArray<String>

All known base event types.

Returns:

  • (Array<String>)

    sorted



61
62
63
# File 'lib/async/matrix/schema.rb', line 61

def event_types
	Registry.instance.event_types
end

.parse(event_hash) ⇒ ApplicationService::Event

Parse a raw event hash into a schema-aware Event.

Parameters:

  • event_hash (Hash)

    the raw event data (string keys)

Returns:



80
81
82
# File 'lib/async/matrix/schema.rb', line 80

def parse(event_hash)
	ApplicationService::Event.new(event_hash)
end

.sizeInteger

Total schemas loaded (base + variants).

Returns:

  • (Integer)


86
87
88
# File 'lib/async/matrix/schema.rb', line 86

def size
	Registry.instance.size
end

.valid?(event_hash) ⇒ Boolean

Boolean validation.

Parameters:

  • event_hash (Hash)

    the raw event data (string keys)

Returns:

  • (Boolean)


55
56
57
# File 'lib/async/matrix/schema.rb', line 55

def valid?(event_hash)
	Registry.instance.valid?(event_hash)
end

.validate(event_hash) ⇒ Array<Hash>

Validate an event hash against its schema.

Parameters:

  • event_hash (Hash)

    the raw event data (string keys)

Returns:

  • (Array<Hash>)

    array of error hashes (empty if valid)



48
49
50
# File 'lib/async/matrix/schema.rb', line 48

def validate(event_hash)
	Registry.instance.validate(event_hash)
end

.variant(event_type, subtype) ⇒ JSONSchemer::Schema?

Look up a variant schema.

Parameters:

  • event_type (String)

    e.g. “m.room.message”

  • subtype (String)

    e.g. “m.text”

Returns:

  • (JSONSchemer::Schema, nil)


41
42
43
# File 'lib/async/matrix/schema.rb', line 41

def variant(event_type, subtype)
	Registry.instance.variant(event_type, subtype)
end

.variant_typesArray<Array(String, String)>

All known variant types as [event_type, subtype] pairs.

Returns:

  • (Array<Array(String, String)>)


67
68
69
# File 'lib/async/matrix/schema.rb', line 67

def variant_types
	Registry.instance.variant_types
end