barrel_mcp_jsonschema (barrel_mcp v3.0.0)
View SourceJSON 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,formatas annotation). - Applicators:
properties,items,allOfand 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
Equivalent to compile(Schema, #{}).
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
Functions
Equivalent to compile(Schema, #{}).
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.
Whether this is a schema we can run at all. Used where a schema is accepted from outside and has to be refused early.
-spec metaschema() -> schema().
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.