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
-
.default_metaschema ⇒ nil, Base + Schema + Schema::MetaSchema
An application-wide default meta-schema set by JSI.default_metaschema=, used by JSI.new_schema to instantiate schemas that do not specify their meta-schema using a
$schema
property. -
.default_metaschema=(default_metaschema) ⇒ Object
Sets JSI.default_metaschema to a schema indicated by the given param.
-
.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.
-
.new_schema_module(schema_content, **kw, &block) ⇒ JSI::SchemaModule
Instantiates the given schema content as a JSI Schema, passing all params to JSI.new_schema, and returns its JSI Schema Module.
-
.schema_registry ⇒ JSI::SchemaRegistry
JSI.schema_registry
is the default SchemaRegistry in which schemas are registered and from which they resolve references. - .schema_registry=(schema_registry) ⇒ Object
Class Method Details
.default_metaschema ⇒ nil, 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.
234 235 236 |
# File 'lib/jsi/schema.rb', line 234 def @default_metaschema end |
.default_metaschema=(default_metaschema) ⇒ Object
Sets default_metaschema to a schema indicated by the given param.
246 247 248 |
# File 'lib/jsi/schema.rb', line 246 def () @default_metaschema = .nil? ? nil : () 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 theschema_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, thedefault_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.
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, } = -> { = if Schema.(, name: "default_metaschema") elsif self. self. 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 .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 = Schema.(id, name: '$schema', schema_registry: schema_registry) .new_schema(schema_content, **new_schema_params, &block) else .call end else .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.
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_registry ⇒ JSI::SchemaRegistry
JSI.schema_registry
is the default SchemaRegistry in which schemas are registered and from
which they resolve references.
104 105 106 |
# File 'lib/jsi.rb', line 104 def self.schema_registry @schema_registry end |
.schema_registry=(schema_registry) ⇒ Object
109 110 111 |
# File 'lib/jsi.rb', line 109 def self.schema_registry=(schema_registry) @schema_registry = schema_registry end |