Module: JSI

Defined in:
lib/jsi.rb,
lib/jsi/ptr.rb,
lib/jsi/base.rb,
lib/jsi/util.rb,
lib/jsi/schema.rb,
lib/jsi/version.rb,
lib/jsi/base/node.rb,
lib/jsi/jsi_coder.rb,
lib/jsi/schema/ref.rb,
lib/jsi/schema_set.rb,
lib/jsi/validation.rb,
lib/jsi/simple_wrap.rb,
lib/jsi/schema/issue.rb,
lib/jsi/util/private.rb,
lib/jsi/util/typelike.rb,
lib/jsi/schema/draft04.rb,
lib/jsi/schema/draft06.rb,
lib/jsi/schema/draft07.rb,
lib/jsi/schema_classes.rb,
lib/jsi/base/mutability.rb,
lib/jsi/metaschema_node.rb,
lib/jsi/schema_registry.rb,
lib/jsi/validation/error.rb,
lib/jsi/schema/validation.rb,
lib/jsi/validation/result.rb,
lib/jsi/schema/application.rb,
lib/jsi/schema/validation/not.rb,
lib/jsi/schema/validation/ref.rb,
lib/jsi/util/private/memo_map.rb,
lib/jsi/schema/validation/enum.rb,
lib/jsi/schema/validation/type.rb,
lib/jsi/schema/validation/array.rb,
lib/jsi/schema/validation/const.rb,
lib/jsi/schema/validation/items.rb,
lib/jsi/schema/validation/object.rb,
lib/jsi/schema/validation/someof.rb,
lib/jsi/schema/validation/string.rb,
lib/jsi/util/private/attr_struct.rb,
lib/jsi/schema/validation/draft04.rb,
lib/jsi/schema/validation/draft06.rb,
lib/jsi/schema/validation/draft07.rb,
lib/jsi/schema/validation/numeric.rb,
lib/jsi/schema/validation/pattern.rb,
lib/jsi/schema/application/draft04.rb,
lib/jsi/schema/application/draft06.rb,
lib/jsi/schema/application/draft07.rb,
lib/jsi/schema/validation/contains.rb,
lib/jsi/schema/validation/required.rb,
lib/jsi/schema/schema_ancestor_node.rb,
lib/jsi/schema/validation/ifthenelse.rb,
lib/jsi/schema/validation/properties.rb,
lib/jsi/schema/validation/dependencies.rb,
lib/jsi/metaschema_node/bootstrap_schema.rb,
lib/jsi/schema/validation/draft04/minmax.rb,
lib/jsi/schema/validation/property_names.rb,
lib/schemas/json-schema.org/draft-04/schema.rb,
lib/schemas/json-schema.org/draft-06/schema.rb,
lib/schemas/json-schema.org/draft-07/schema.rb,
lib/jsi/schema/application/child_application.rb,
lib/jsi/schema/application/inplace_application.rb,
lib/jsi/schema/application/child_application/items.rb,
lib/jsi/schema/application/inplace_application/ref.rb,
lib/jsi/schema/application/child_application/draft04.rb,
lib/jsi/schema/application/child_application/draft06.rb,
lib/jsi/schema/application/child_application/draft07.rb,
lib/jsi/schema/application/child_application/contains.rb,
lib/jsi/schema/application/inplace_application/someof.rb,
lib/jsi/schema/application/inplace_application/draft04.rb,
lib/jsi/schema/application/inplace_application/draft06.rb,
lib/jsi/schema/application/inplace_application/draft07.rb,
lib/jsi/schema/application/child_application/properties.rb,
lib/jsi/schema/application/inplace_application/ifthenelse.rb,
lib/jsi/schema/application/inplace_application/dependencies.rb

Defined Under Namespace

Modules: JSONSchemaDraft04, JSONSchemaDraft06, JSONSchemaDraft07, JSONSchemaOrgDraft04, JSONSchemaOrgDraft06, JSONSchemaOrgDraft07, Schema, SchemaClasses, SimpleWrap, Util, Validation Classes: Base, Bug, JSICoder, MetaSchemaNode, Ptr, SchemaModule, SchemaRegistry, SchemaSet

Constant Summary collapse

DEFAULT_CONTENT_TO_IMMUTABLE =
proc do |content|
  Util.deep_to_frozen(content, not_implemented: proc do |instance|
    raise(ArgumentError, [
      "JSI does not know how to make the given instance immutable.",
      "See new_jsi / new_schema params `mutable` and `to_immutable` documentation for options.",
      "https://www.rubydoc.info/gems/jsi/#{VERSION}/JSI/SchemaSet#new_jsi-instance_method",
      "Given instance: #{instance.pretty_inspect.chomp}",
    ].join("\n"))
  end)
end
DEFAULT_SCHEMA_REGISTRY =
SchemaRegistry.new.tap do |schema_registry|
  schema_registry.autoload_uri("http://json-schema.org/draft-04/schema") { JSI::JSONSchemaDraft04.schema }
  schema_registry.autoload_uri("http://json-schema.org/draft-06/schema") { JSI::JSONSchemaDraft06.schema }
  schema_registry.autoload_uri("http://json-schema.org/draft-07/schema") { JSI::JSONSchemaDraft07.schema }
end.freeze
VERSION =
"0.8.1".freeze
MetaschemaNode =
Deprecated.

after v0.7.0, alias of MetaSchemaNode

MetaSchemaNode

Class Method Summary collapse

Class Method Details

.default_metaschemanil, Base + Schema + Schema::MetaSchema

An application-wide default meta-schema set by default_metaschema=, used by new_schema to instantiate schemas that do not specify their meta-schema using a $schema property.

Returns:



234
235
236
# File 'lib/jsi/schema.rb', line 234

def default_metaschema
  @default_metaschema
end

.default_metaschema=(default_metaschema) ⇒ Object

Sets default_metaschema to a schema indicated by the given param.

Parameters:

  • default_metaschema (Schema::MetaSchema, SchemaModule::MetaSchemaModule, #to_str, nil)

    Indicates the default meta-schema. This may be a meta-schema or a meta-schema's schema module (e.g. JSI::JSONSchemaDraft07), or a URI (as would be in a $schema keyword).

    nil to unset.



246
247
248
# File 'lib/jsi/schema.rb', line 246

def default_metaschema=(default_metaschema)
  @default_metaschema = default_metaschema.nil? ? nil : ensure_metaschema(default_metaschema)
end

.new_schema(schema_content, default_metaschema: nil, 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.

The meta-schema that describes the schema must be indicated:

  • If the schema object has a $schema property, that URI is resolved using the schema_registry param (by default schema_registry), and that meta-schema is used. For example:
  JSI.new_schema({
    "$schema" => "http://json-schema.org/draft-07/schema#",
    "properties" => ...,
  })
  • if no $schema property is present, the default_metaschema param is used, if the caller specifies it. For example:
  JSI.new_schema({"properties" => ...}, default_metaschema: JSI::JSONSchemaDraft07)
  • if no default_metaschema param is specified, the application-wide default JSI.default_metaschema is used, if the application has set it. For example:
  JSI.default_metaschema = JSI::JSONSchemaDraft07
  JSI.new_schema({"properties" => ...})

An ArgumentError is raised if none of these indicates a meta-schema to use.

Note that if you are instantiating a schema known to have no $schema property, an alternative to specifying a default_metaschema is to call new_schema on the meta-schema or its schema module, e.g. JSI::JSONSchemaDraft07.new_schema(my_schema_content)

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

Parameters:

  • default_metaschema (Schema::MetaSchema, SchemaModule::MetaSchemaModule, #to_str) (defaults to: nil)

    Indicates the meta-schema to use if the given schema_content does not have a $schema property. This may be a meta-schema or a meta-schema's schema module (e.g. JSI::JSONSchemaDraft07), or a URI (as would be in a $schema keyword).

  • 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 Schema whose content comes from the given schema_content and whose schemas are inplace applicators of the indicated meta-schema



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/jsi/schema.rb', line 304

def new_schema(schema_content,
    default_metaschema: nil,
    # params of Schema::MetaSchema#new_schema have their default values repeated here. delegating in a splat
    # would remove repetition, but yard doesn't display delegated defaults with its (see X) directive.
    uri: nil,
    register: true,
    schema_registry: JSI.schema_registry,
    stringify_symbol_keys: true,
    to_immutable: DEFAULT_CONTENT_TO_IMMUTABLE,
    &block
)
  new_schema_params = {
    uri: uri,
    register: register,
    schema_registry: schema_registry,
    stringify_symbol_keys: stringify_symbol_keys,
    to_immutable: to_immutable,
  }
  default_metaschema_new_schema = -> {
    default_metaschema = if default_metaschema
      Schema.ensure_metaschema(default_metaschema, name: "default_metaschema")
    elsif self.default_metaschema
      self.default_metaschema
    else
      raise(ArgumentError, [
        "When instantiating a schema with no `$schema` property, you must specify its meta-schema by one of these methods:",
        "- pass the `default_metaschema` param to this method",
        "  e.g.: JSI.new_schema(..., default_metaschema: JSI::JSONSchemaDraft07)",
        "- invoke `new_schema` on the appropriate meta-schema or its schema module",
        "  e.g.: JSI::JSONSchemaDraft07.new_schema(...)",
        "- set JSI.default_metaschema to an application-wide default meta-schema initially",
        "  e.g.: JSI.default_metaschema = JSI::JSONSchemaDraft07",
        "instantiating schema_content: #{schema_content.pretty_inspect.chomp}",
      ].join("\n"))
    end
    default_metaschema.new_schema(schema_content, **new_schema_params, &block)
  }
  if schema_content.is_a?(Schema)
    raise(TypeError, [
      "Given schema_content is already a JSI::Schema. It cannot be instantiated as the content of a schema.",
      "given: #{schema_content.pretty_inspect.chomp}",
    ].join("\n"))
  elsif schema_content.is_a?(JSI::Base)
    raise(TypeError, [
      "Given schema_content is a JSI::Base. It cannot be instantiated as the content of a schema.",
      "given: #{schema_content.pretty_inspect.chomp}",
    ].join("\n"))
  elsif schema_content.respond_to?(:to_hash)
    id = schema_content['$schema'] || stringify_symbol_keys && schema_content[:'$schema']
    if id
      unless id.respond_to?(:to_str)
        raise(ArgumentError, "given schema_content keyword `$schema` is not a string")
      end
      metaschema = Schema.ensure_metaschema(id, name: '$schema', schema_registry: schema_registry)
      metaschema.new_schema(schema_content, **new_schema_params, &block)
    else
      default_metaschema_new_schema.call
    end
  else
    default_metaschema_new_schema.call
  end
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:



68
69
70
# File 'lib/jsi.rb', line 68

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

.schema_registryJSI::SchemaRegistry

JSI.schema_registry is the default SchemaRegistry in which schemas are registered and from which they resolve references.

Returns:



104
105
106
# File 'lib/jsi.rb', line 104

def self.schema_registry
  @schema_registry
end

.schema_registry=(schema_registry) ⇒ Object

Parameters:



109
110
111
# File 'lib/jsi.rb', line 109

def self.schema_registry=(schema_registry)
  @schema_registry = schema_registry
end