JSV.Resolver behaviour (jsv v0.7.2)

View Source

A behaviour describing the implementation of a [guides/build/custom resolver. Resolves remote resources when building a JSON schema.

Summary

Callbacks

Receives an URI and the options passed in the resolver tuple to JSV.build/2 and returns a result tuple for a raw JSON schema map.

Functions

Returns a new resolver, with the given behaviour implementations, and a default meta-schema URL to use with schemas that do not declare a $schema property.

Returns the raw schema identified by the given key if was previously resolved.

Returns the $vocabulary property of a schema identified by its namespace.

Fetches the remote resource into the internal resolver cache and returns a new resolver with that updated cache.

Adds the given raw schema as a pre-resolved schema, using the :root namespace if the schema does not contain a $id property.

Types

resolvable()

@type resolvable() :: JSV.Key.ns() | JSV.Key.pointer() | JSV.Ref.t()

t()

@opaque t()

Callbacks

resolve(uri, opts)

@callback resolve(uri :: String.t(), opts :: term()) ::
  {:ok, map()} | {:normal, map()} | {:error, term()}

Receives an URI and the options passed in the resolver tuple to JSV.build/2 and returns a result tuple for a raw JSON schema map.

Returning boolean schemas from resolvers is not supported. You may wrap the boolean value in a $defs or any other pointer as a workaround.

Schemas will be normalized using JSV.Schema.normalize/1. If the resolver returns schema that are already in JSON-decoded form (like a response body from an HTTP call) without atoms, module names or structs, the resolver implementation can return {:normal, map} instead to skip the normalization.

Functions

chain_of(resolvers, default_meta)

@spec chain_of([{module(), term()}], binary()) :: t()

Returns a new resolver, with the given behaviour implementations, and a default meta-schema URL to use with schemas that do not declare a $schema property.

fetch_resolved(rsv, pointer)

@spec fetch_resolved(t(), resolvable() | {:meta, resolvable()}) ::
  {:ok, JSV.Resolver.Resolved.t() | {:alias_of, JSV.Key.t()}} | {:error, term()}

Returns the raw schema identified by the given key if was previously resolved.

fetch_vocabulary(rsv, meta)

@spec fetch_vocabulary(t(), binary()) ::
  {:ok, %{optional(binary()) => boolean()}} | {:error, term()}

Returns the $vocabulary property of a schema identified by its namespace.

The schema must have been resolved previously as a meta-schema (i.e. found in an $schema property of a resolved schema).

resolve(rsv, resolvable)

@spec resolve(t(), resolvable() | {:prefetched, term(), term()}) ::
  {:ok, t()} | {:error, term()}

Fetches the remote resource into the internal resolver cache and returns a new resolver with that updated cache.

resolve_root(rsv, raw_schema)

@spec resolve_root(t(), JSV.raw_schema()) ::
  {:ok, :root | binary(), t()} | {:error, term()}

Adds the given raw schema as a pre-resolved schema, using the :root namespace if the schema does not contain a $id property.