barrel_mcp_jsonschema (barrel_mcp v3.0.1)

View Source

JSON Schema 2020-12 validator.

MCP describes tool inputs and outputs with 2020-12 schemas, so a subset validator either rejects valid data or accepts invalid data depending on which keyword it skipped. This implements the dialect: every assertion keyword, $ref/$defs/$anchor, the dynamic pair, and the unevaluated* keywords that need annotations to answer.

Two phases

compile/1 resolves identifiers and references once and returns something validate/2 can run repeatedly. A schema that cannot be resolved is rejected there rather than failing halfway through a validation.

No network

A $ref to an absolute URI we were not given is an error, never a fetch. Supply such documents through compile/2's registry. This is a MUST in the spec, and it is also what stops a schema from turning into an outbound request.

Bounds

Depth, subschema count and reference expansions are capped, so a hostile schema cannot spend the caller's stack or time.

Sections, in file order

  • Compilation: identifiers, anchors, references, the registry.
  • Validation: the entry point and the per-schema walk with annotations.
  • Assertions: the leaf keywords (type, enum, bounds, patterns, format as annotation).
  • Applicators: properties, items, allOf and friends.
  • unevaluated*: the keywords that read the annotations the walk collected.
  • URIs and pointers, and the dialect's own metaschema.

Pure functions throughout; nothing here spawns or stores.

Summary

Functions

Resolve a schema's identifiers and check every reference.

Whether this is a schema we can run at all. Used where a schema is accepted from outside and has to be refused early.

The 2020-12 metaschema, compiled. Use it to check that something claiming to be a schema is one.

Validate Value against a schema, compiling it first.

Validate against an already compiled schema.

Whether Schema is a valid 2020-12 schema, by the dialect's own rules rather than by whether we happen to understand it.

Types

error/0

-type error() :: {path(), atom() | {atom(), term()}}.

path/0

-type path() :: [binary() | non_neg_integer()].

schema/0

-opaque schema()

Functions

compile(Schema)

-spec compile(term()) -> {ok, schema()} | {error, term()}.

Equivalent to compile(Schema, #{}).

compile(Schema, Registry0)

-spec compile(term(), #{binary() => term()}) -> {ok, schema()} | {error, term()}.

Resolve a schema's identifiers and check every reference.

Registry maps absolute URIs to schema documents, for the $refs that name something outside this one. Anything not in it is an error: nothing is fetched.

is_valid_schema(Schema)

-spec is_valid_schema(term()) -> boolean().

Whether this is a schema we can run at all. Used where a schema is accepted from outside and has to be refused early.

metaschema()

-spec metaschema() -> schema().

The 2020-12 metaschema, compiled. Use it to check that something claiming to be a schema is one.

validate(Value, Schema)

-spec validate(term(), term()) -> ok | {error, [error()]}.

Validate Value against a schema, compiling it first.

validate(Value, Schema, Opts)

-spec validate(term(), schema(), map()) -> ok | {error, [error()]}.

Validate against an already compiled schema.

validate_schema(Schema)

-spec validate_schema(term()) -> ok | {error, [error()]}.

Whether Schema is a valid 2020-12 schema, by the dialect's own rules rather than by whether we happen to understand it.