Class: Familia::SchemaRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/familia/schema_registry.rb

Overview

Registry for loading and caching external JSON schema files. Schemas are loaded at boot time based on configuration.

Examples:

Convention-based loading

Familia.schema_path = 'schemas/models'
SchemaRegistry.load!
SchemaRegistry.schema_for('Customer')  # loads schemas/models/customer.json

Explicit mapping

Familia.schemas = { 'Customer' => 'schemas/customer.json' }
SchemaRegistry.load!

Class Method Summary collapse

Class Method Details

.load!Object

Load schemas based on current configuration. Safe to call multiple times - only loads once.



24
25
26
27
28
29
30
31
# File 'lib/familia/schema_registry.rb', line 24

def load!
  return if @loaded

  @schemas ||= {}
  load_from_path if Familia.schema_path
  load_from_hash if Familia.schemas&.any?
  @loaded = true
end

.loaded?Boolean

Check if schemas have been loaded

Returns:

  • (Boolean)


34
35
36
# File 'lib/familia/schema_registry.rb', line 34

def loaded?
  @loaded == true
end

.reset!Object

Reset registry (primarily for testing)



80
81
82
83
84
# File 'lib/familia/schema_registry.rb', line 80

def reset!
  @schemas = {}
  @loaded = false
  @validator = nil
end

.schema_defined?(klass_or_name) ⇒ Boolean

Check if a schema is defined for the given class

Returns:

  • (Boolean)


48
49
50
# File 'lib/familia/schema_registry.rb', line 48

def schema_defined?(klass_or_name)
  !schema_for(klass_or_name).nil?
end

.schema_for(klass_or_name) ⇒ Hash?

Get schema for a class by name or class reference

Parameters:

  • klass_or_name (Class, String)

    the class or class name

Returns:

  • (Hash, nil)

    the parsed JSON schema or nil



41
42
43
44
45
# File 'lib/familia/schema_registry.rb', line 41

def schema_for(klass_or_name)
  load! unless loaded?
  name = klass_or_name.is_a?(Class) ? klass_or_name.name : klass_or_name.to_s
  @schemas[name]
end

.schemasObject

All registered schemas



53
54
55
56
# File 'lib/familia/schema_registry.rb', line 53

def schemas
  load! unless loaded?
  @schemas.dup
end

.validate(klass_or_name, data) ⇒ Hash

Validate data against a schema

Parameters:

  • klass_or_name (Class, String)

    the class whose schema to use

  • data (Hash)

    the data to validate

Returns:

  • (Hash)

    { valid: Boolean, errors: Array }



62
63
64
65
66
67
68
# File 'lib/familia/schema_registry.rb', line 62

def validate(klass_or_name, data)
  schema = schema_for(klass_or_name)
  return { valid: true, errors: [] } unless schema

  errors = validator.validate(schema, data).to_a
  { valid: errors.empty?, errors: errors }
end

.validate!(klass_or_name, data) ⇒ Object

Validate data or raise SchemaValidationError

Raises:



72
73
74
75
76
77
# File 'lib/familia/schema_registry.rb', line 72

def validate!(klass_or_name, data)
  result = validate(klass_or_name, data)
  raise SchemaValidationError.new(result[:errors]) unless result[:valid]

  true
end