Module: JSI::Schema::DescribesSchema

Defined in:
lib/jsi/schema.rb

Overview

This module extends any JSI Schema which describes schemas.

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

Schemas which describes schemas include JSI::Schema in their JSI Schema module, so for a schema which is an instance of DescribesSchema, 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 Method Summary collapse

Instance Method Details

#new_schema(schema_content, uri: nil, register: true, schema_registry: JSI.schema_registry, stringify_symbol_keys: true) { ... } ⇒ 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.

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.

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.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/jsi/schema.rb', line 183

def new_schema(schema_content,
    uri: nil,
    register: true,
    schema_registry: JSI.schema_registry,
    stringify_symbol_keys: true,
    &block
)
  schema_jsi = new_jsi(schema_content,
    uri: uri,
    register: register,
    schema_registry: schema_registry,
    stringify_symbol_keys: stringify_symbol_keys,
  )
  if block
    schema_jsi.jsi_schema_module_exec(&block)
  end
  schema_jsi
end

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

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

Returns:



206
207
208
# File 'lib/jsi/schema.rb', line 206

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