Class: Schemurai::SchemaRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/schemurai.rb,
sig/schemurai.rbs

Overview

Compiles schemas that share registered external schemas and backend state.

Register every external schema with schemas: before compiling. A registry can be made shareable once registration and compilation are complete.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schemas: {}, validate_schema: false, backend: Backend.requested) ⇒ SchemaRegistry

Creates a registry.

schemas maps absolute URI strings to JSON-shaped Ruby schemas. Pass validate_schema: true to validate every registered and compiled schema against its meta-schema. backend accepts :ruby, :vm, or :default.

Parameters:

  • schemas: (schemas) (defaults to: {})
  • validate_schema: (Boolean) (defaults to: false)
  • backend: (backend_selection) (defaults to: Backend.requested)


83
84
85
86
87
88
89
# File 'lib/schemurai.rb', line 83

def initialize(schemas: {}, validate_schema: false, backend: Backend.requested)
  @backend = Backend.resolve(backend)
  @validate_schema = validate_schema
  @graph = Internal::SchemaGraph.new(schemas: schemas)
  @compiler = VM::Compiler.new(@graph) if @backend == :vm
  schemas.each_value { |schema| validate_schema!(schema) } if validate_schema?
end

Instance Attribute Details

#backend ⇒ backend (readonly)

The selected backend, either :ruby or :vm.

Returns:



76
77
78
# File 'lib/schemurai.rb', line 76

def backend
  @backend
end

Instance Method Details

#compile(schema, base_uri: nil, content: false, format: false) ⇒ Validator

Compiles schema and returns a Validator.

base_uri supplies the schema's base URI. Pass content: true or format: true to enable the corresponding optional assertions.

Parameters:

  • schema (schema)
  • base_uri: (String, nil) (defaults to: nil)
  • content: (Boolean) (defaults to: false)
  • format: (Boolean) (defaults to: false)

Returns:

Raises:



100
101
102
103
104
105
106
# File 'lib/schemurai.rb', line 100

def compile(schema, base_uri: nil, content: false, format: false)
  raise Error, "cannot compile schemas after the registry is made shareable" if shareable?

  validate_schema!(schema) if validate_schema?
  root = @graph.compile(schema, base_uri: base_uri)
  build_validator(root, content: content, format: format)
end

#make_shareable ⇒ self

Resolves and freezes registered schemas, then makes the registry Ractor-shareable.

Returns self. No more schemas can be compiled after this transition.

Returns:

  • (self)


125
126
127
128
129
130
131
# File 'lib/schemurai.rb', line 125

def make_shareable
  return self if shareable?

  @graph.make_shareable
  @compiler&.compile_all
  Ractor.make_shareable(self)
end

#shareable? ⇒ Boolean

Returns whether this registry is Ractor-shareable.

Returns:

  • (Boolean)


134
135
136
# File 'lib/schemurai.rb', line 134

def shareable?
  Ractor.shareable?(self)
end

#valid_schema?(schema) ⇒ Boolean

Returns whether schema conforms to its declared meta-schema.

Parameters:

  • schema (schema)

Returns:

  • (Boolean)


118
119
120
# File 'lib/schemurai.rb', line 118

def valid_schema?(schema)
  validate_schema(schema).valid?
end

#validate_schema(schema) ⇒ Result

Validates schema against its declared meta-schema and returns a Result. Schemas without $schema are validated as Draft 7.

Parameters:

  • schema (schema)

Returns:

Raises:



110
111
112
113
114
115
# File 'lib/schemurai.rb', line 110

def validate_schema(schema)
  raise Error, "cannot validate schemas after the registry is made shareable" if shareable?

  root = @graph.meta_schema_root(schema)
  build_validator(root, content: false, format: false).validate(schema)
end

#validate_schema? ⇒ Boolean

Returns whether this registry validates every schema against its meta-schema.

Returns:

  • (Boolean)


92
93
94
# File 'lib/schemurai.rb', line 92

def validate_schema?
  @validate_schema
end

#validator_for(uri, content: false, format: false) ⇒ Validator

Returns a Validator for the registered resource at uri.

The registry must first be made shareable with #make_shareable.

Parameters:

  • uri (String)
  • content: (Boolean) (defaults to: false)
  • format: (Boolean) (defaults to: false)

Returns:

Raises:



141
142
143
144
145
146
147
148
# File 'lib/schemurai.rb', line 141

def validator_for(uri, content: false, format: false)
  raise Error, "make_shareable must be called before retrieving validators by URI" unless shareable?

  root = @graph.node_at(uri)
  raise ResolutionError, "unregistered schema URI #{uri.inspect}" unless root

  build_validator(root, content: content, format: format)
end