Alternera.Plugin.Provisional.JsonSchema (alternera_plugin_sdk v0.9.0)

Copy Markdown View Source

Provisional: added for Validate JSON and the proxy's json_schema test assertions, so that every JSON Schema check reads a schema the same way, reports a mismatch the same way and compiles a schema once. A change to it needs a changelog line but no new major (docs/USER_PLUGINS_DESIGN.md).

check/1 refuses a schema that cannot be built, with its reason. validate/2 checks a decoded JSON value against a schema, compiling it once per distinct schema (Provisional.Cache, name "json_schema", about 512 entries); run/2 does the same for a schema not checked before, answering {:invalid_schema, reason} when it does not build.

Dialects

$schema names the dialect, and a schema without one is draft 7:

  • 2020-12 and draft 7 are read by JSV;
  • 2019-09 is read as 2020-12 (JSV has no 2019-09; the two differ in $recursiveRef, which few schemas use), an embedded resource's $schema as well as the root's;
  • drafts 4 and 6 are read by JsonXema, as they were before JSV;
  • any other $schema is refused.

A known format is asserted (email, date-time, uuid, uri and the rest JSV knows); an unknown one is ignored, as JsonXema ignored it. No $ref is fetched: a reference resolves inside the schema or the schema is refused.

Registry references

{"$ref": "registry:<name>"} names a schema the tenant stores. The host resolves it before a plugin sees the settings of a field marked json_schema: (Plugin.schema_field): every schema the references reach is embedded under the root's $defs (definitions in draft 7) with "$id": "registry:<name>", and the reference resolves to it. A root in draft 4 or 6 cannot hold one.

check/1 reads a reference nothing embeds as a schema that allows everything, so a step whose stored settings name the registry is checked for everything else where no tenant is at hand (a save's checks, the editor). validate/2 and run/2 never do: a reference left unresolved when the value is checked refuses the schema.

Errors

An error names where in the value a constraint failed and what the schema asked for ($.texts[0]: type must be string), never the value itself: errors are recorded on executions and test runs and can reach an answer, and the value is the body. A member name the body chose (under additionalProperties, patternProperties or propertyNames) is shown as * when it is long or holds a control character, so a body cannot write into the error text.

Summary

Functions

:ok when schema builds, or {:error, reason}. A registry reference nothing in the schema embeds stands for a schema that allows everything (see "Registry references").

The registry names schema refers to with {"$ref": "registry:<name>"}, sorted, without the prefix. A $ref inside const, enum, default or examples is data, not a reference.

validate/2 for a schema not checked before: {:invalid_schema, reason} when it does not build, with one cache lookup either way.

errors as one line, the first shown of them as path: message joined by ;, and how many were left out.

The registry names schema refers to that it does not embed (no "$id": "registry:<name>" in it): what the host still has to resolve.

Checks value (decoded JSON) against schema: :ok, or {:error, errors} with one entry per failed constraint, in a stable order. Raises ArgumentError when the schema does not build; check/1 first, or run/2.

validate/2 of a body read through its document (Plugin.JsonDoc) rather than decoded whole, with the same answer, errors and their order included: the schema is split where its keywords allow, and each piece of the value is decoded and checked on its own (see "Checking a document"). :whole when the schema cannot be split at the root, or is in draft 4 or 6: the caller decodes the body whole, under its own bounds, and calls validate/2. Raises as validate/2 does.

Types

error()

@type error() :: %{path: String.t(), message: String.t()}

Functions

check(schema)

@spec check(map()) :: :ok | {:error, String.t()}

:ok when schema builds, or {:error, reason}. A registry reference nothing in the schema embeds stands for a schema that allows everything (see "Registry references").

registry_references(schema)

@spec registry_references(term()) :: [String.t()]

The registry names schema refers to with {"$ref": "registry:<name>"}, sorted, without the prefix. A $ref inside const, enum, default or examples is data, not a reference.

run(schema, value)

@spec run(map(), term()) :: :ok | {:error, [error()]} | {:invalid_schema, String.t()}

validate/2 for a schema not checked before: {:invalid_schema, reason} when it does not build, with one cache lookup either way.

summary(errors, shown \\ 10)

@spec summary([error()], pos_integer()) :: String.t()

errors as one line, the first shown of them as path: message joined by ;, and how many were left out.

unresolved_references(schema)

@spec unresolved_references(term()) :: [String.t()]

The registry names schema refers to that it does not embed (no "$id": "registry:<name>" in it): what the host still has to resolve.

validate(schema, value)

@spec validate(map(), term()) :: :ok | {:error, [error()]}

Checks value (decoded JSON) against schema: :ok, or {:error, errors} with one entry per failed constraint, in a stable order. Raises ArgumentError when the schema does not build; check/1 first, or run/2.

validate_doc(schema, doc, opts \\ [])

@spec validate_doc(map(), Alternera.Plugin.JsonDoc.t(), keyword()) ::
  :ok | {:error, [error()]} | :whole

validate/2 of a body read through its document (Plugin.JsonDoc) rather than decoded whole, with the same answer, errors and their order included: the schema is split where its keywords allow, and each piece of the value is decoded and checked on its own (see "Checking a document"). :whole when the schema cannot be split at the root, or is in draft 4 or 6: the caller decodes the body whole, under its own bounds, and calls validate/2. Raises as validate/2 does.

Checking a document

An object whose schema holds only type, properties, additionalProperties, required, minProperties, maxProperties, propertyNames, dependentRequired, annotations and keywords that apply to other types only is checked as its member names (every value null) against those keywords, then each member's value against its properties entry or additionalProperties, the same way. An array whose schema holds only type, items (one schema), minItems, maxItems and the like is checked as its length, then each element against items. A piece of at most :small_piece_bytes (4 KiB by default, never more than :max_piece_bytes), the whole document included, is decoded and checked in one call instead of walked. Any other value, or one whose schema holds any other keyword (enum, allOf, $ref, patternProperties, uniqueItems, an unknown one), is decoded and checked whole, so peak memory is the largest such piece; one over :max_piece_bytes (1 MiB by default) makes the answer :whole, for the caller to read the body under its bounds. A repeated key counts once, its last value, as the decode does. A schema with a $ref other than into the root's $defs or definitions (by pointer, or by the $id of one of their entries, as a registry reference resolves), an $id below the root but such an entry's, an anchor or a dynamic reference is not split at all.