Module: JSI::Schema::MetaSchema

Defined in:
lib/jsi/schema.rb

Overview

This module extends any JSI Schema that is a meta-schema, i.e. it describes schemas.

Examples of a meta-schema include the JSON Schema meta-schemas and the OpenAPI schema definition which describes "A deterministic version of a JSON Schema object."

Meta-schemas include JSI::Schema in their JSI Schema module, so for a schema which is an instance of JSI::Schema::MetaSchema, instances of that schema are instances of JSI::Schema and are schemas.

A schema is indicated as describing other schemas using the #describes_schema! method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#schema_implementation_modulesSet<Module> (readonly)

Returns:

  • (Set<Module>)


148
149
150
# File 'lib/jsi/schema.rb', line 148

def schema_implementation_modules
  @schema_implementation_modules
end

Instance Method Details

#new_schema(schema_content, uri: nil, register: true, schema_registry: JSI.schema_registry, stringify_symbol_keys: true, to_immutable: DEFAULT_CONTENT_TO_IMMUTABLE) { ... } ⇒ JSI::Base subclass + JSI::Schema

Instantiates the given schema content as a JSI Schema.

By default, the schema will be registered with the JSI.schema_registry. This can be controlled by params register and schema_registry.

By default, the schema_content will have any Symbol keys of Hashes replaced with Strings (recursively through the document). This is controlled by the param stringify_symbol_keys.

Schemas instantiated with new_schema are immutable, their content transformed using the to_immutable param.

Parameters:

  • schema_content

    an object to be instantiated as a JSI Schema - typically a Hash

  • uri (#to_str, Addressable::URI) (defaults to: nil)

    The retrieval URI of the schema document. If specified, the root schema will be identified by this URI, in addition to any absolute URI declared with an id keyword, for resolution in the schema_registry.

    It is rare that this needs to be specified. Most schemas, if they use absolute URIs, will use the $id keyword (id in draft 4) to specify this. A different retrieval URI is useful in unusual cases:

    • A schema in the document uses relative URIs for $id or $ref without an absolute id in an ancestor schema - these will be resolved relative to this URI
    • Another schema refers with $ref to the schema being instantiated by this retrieval URI, rather than an id declared in the schema - the schema is resolvable by this URI in the schema_registry.
  • register (Boolean) (defaults to: true)

    Whether the instantiated schema and any subschemas with absolute URIs will be registered in the schema registry indicated by param schema_registry.

  • schema_registry (SchemaRegistry, nil) (defaults to: JSI.schema_registry)

    The registry this schema will use.

    • The schema and subschemas will be registered here with any declared URI, unless the register param is false.
    • References from within the schema (typically from $ref keywords) are resolved using this registry.
  • stringify_symbol_keys (Boolean) (defaults to: true)

    Whether the schema content will have any Symbol keys of Hashes replaced with Strings (recursively through the document). Replacement is done on a copy; the given schema content is not modified.

  • to_immutable (#call, nil) (defaults to: DEFAULT_CONTENT_TO_IMMUTABLE)

    A proc/callable which takes given instance content and results in an immutable (i.e. deeply frozen) object equal to that. If the instantiated JSI will be mutable, this is not used. Though not recommended, this may be nil with immutable JSIs if the instance content is otherwise guaranteed to be immutable, as well as any modified copies of the instance.

Yields:

  • If a block is given, it is evaluated in the context of the schema's JSI schema module using Module#module_exec.

Returns:

  • (JSI::Base subclass + JSI::Schema)

    a JSI which is a JSI::Schema whose content comes from the given schema_content and whose schemas are this schema's inplace applicators.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/jsi/schema.rb', line 190

def new_schema(schema_content,
    uri: nil,
    register: true,
    schema_registry: JSI.schema_registry,
    stringify_symbol_keys: true,
    to_immutable: DEFAULT_CONTENT_TO_IMMUTABLE,
    &block
)
  schema_jsi = new_jsi(schema_content,
    uri: uri,
    register: register,
    schema_registry: schema_registry,
    stringify_symbol_keys: stringify_symbol_keys,
    to_immutable: to_immutable,
    mutable: false,
  )

  schema_jsi.jsi_schema_module_exec(&block) if block

  schema_jsi
end

#new_schema_module(schema_content, **kw, &block) ⇒ JSI::SchemaModule

Instantiates the given schema content as a JSI Schema, passing all params to #new_schema, and returns its JSI Schema Module.

Returns:



216
217
218
# File 'lib/jsi/schema.rb', line 216

def new_schema_module(schema_content, **kw, &block)
  new_schema(schema_content, **kw, &block).jsi_schema_module
end