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/metaschema.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/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, Metaschema, Schema, SchemaClasses, SimpleWrap, Util, Validation Classes: Base, Bug, JSICoder, MetaschemaNode, Ptr, SchemaModule, SchemaRegistry, SchemaSet

Constant Summary collapse

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.7.0".freeze

Class Method Summary collapse

Class Method Details

.default_metaschemanil, Base + Schema + Schema::DescribesSchema

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

Returns:



224
225
226
# File 'lib/jsi/schema.rb', line 224

def default_metaschema
  @default_metaschema
end

.default_metaschema=(default_metaschema) ⇒ Object

Sets default_metaschema to a schema indicated by the given param.

Parameters:



236
237
238
# File 'lib/jsi/schema.rb', line 236

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

.new_metaschema(metaschema_document, schema_implementation_modules:) ⇒ JSI::MetaschemaNode + JSI::DescribesSchema + JSI::Schema

Instantiates the given document as a JSI Metaschema.

Parameters:

  • metaschema_document

    an object to be instantiated as a JSI Metaschema

  • schema_implementation_modules (Enumerable<Module>)

    modules which implement the functionality of the schema. these are included on the JSI::Schema#jsi_schema_module of the metaschema. they extend any schema described by the metaschema, including those in the document containing the metaschema, and the metaschema itself. see JSI::Schema#describes_schema! param schema_implementation_modules.

Returns:



67
68
69
70
71
72
73
# File 'lib/jsi.rb', line 67

def self.new_metaschema(metaschema_document,
    schema_implementation_modules:
)
  MetaschemaNode.new(metaschema_document,
    schema_implementation_modules: schema_implementation_modules,
  )
end

.new_metaschema_module(metaschema_document, **kw) ⇒ Module + JSI::SchemaModule::DescribesSchemaModule + JSI::SchemaModule

Instantiates the given document as a JSI Metaschema, passing all params to new_metaschema, and returns its JSI Schema Module.



79
80
81
# File 'lib/jsi.rb', line 79

def self.new_metaschema_module(metaschema_document, **kw)
  new_metaschema(metaschema_document, **kw).jsi_schema_module
end

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

The metaschema which 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 metaschema 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 indicate a metaschema 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 metaschema or its schema module, e.g. JSI::JSONSchemaDraft07.new_schema(my_schema_content)

Parameters:

  • default_metaschema (Schema::DescribesSchema, SchemaModule::DescribesSchemaModule, #to_str) (defaults to: nil)

    Indicates the metaschema to use if the given schema_content does not have a $schema property. This may be a metaschema or a metaschema'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.
  • 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 metaschema



290
291
292
293
294
295
296
297
298
299
300
301
302
303
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
# File 'lib/jsi/schema.rb', line 290

def new_schema(schema_content,
    default_metaschema: nil,
    # params of DescribesSchema#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,
    &block
)
  new_schema_params = {
    uri: uri,
    register: register,
    schema_registry: schema_registry,
    stringify_symbol_keys: stringify_symbol_keys,
  }
  default_metaschema_new_schema = -> {
    default_metaschema = if default_metaschema
      Schema.ensure_describes_schema(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 metaschema 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 metaschema or its schema module",
        "  e.g.: JSI::JSONSchemaDraft07.new_schema(...)",
        "- set JSI.default_metaschema to an application-wide default metaschema 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_describes_schema(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) ⇒ 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:



58
59
60
# File 'lib/jsi.rb', line 58

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:



87
88
89
# File 'lib/jsi.rb', line 87

def self.schema_registry
  @schema_registry
end

.schema_registry=(schema_registry) ⇒ Object

Parameters:



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

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