Snodo.Schema.Validator.JSV (snodo_jsv v0.2.0)

Copy Markdown View Source

Optional JSV-backed JSON Schema validation with an offline, non-casting policy.

Install the separate snodo_jsv integration and select this module with use Snodo.Server, schema_validator: Snodo.Schema.Validator.JSV.

Draft 2020-12 is the default; explicit Draft 7 is also supported. Schemas are checked against their bundled meta-schema before compilation. Remote schemas, custom dialects, mixed dialects, and JSV casting extensions are rejected. Unknown annotation keywords are otherwise preserved. The safety scan treats schema-control and casting keywords conservatively even in annotation data.

validate/2 compiles on each call and has no global cache. Applications can retain an immutable root from compile/1 or compile/2 and use validate_compiled/2 in their own validator module for a fixed catalog. Neither operation returns transformed data or changes advertised schemas.

Invalid instances return {:error, reason}. Schema build failures raise Snodo.Schema.Validator.JSV.BuildError from validate/2, so the MCP router reports a server configuration error rather than blaming client arguments. compile/2 returns those errors as tuples for startup-time admission.

Summary

Functions

Compiles a JSON-decoded schema without network access or data casting.

Validates an instance using an immutable root; the original value is never replaced.

Functions

compile(schema, options \\ [])

Compiles a JSON-decoded schema without network access or data casting.

The only option is formats: :annotation | :assertion. The default :annotation follows the bundled 2020-12 dialect; explicit assertion uses JSV's built-in format validators and rejects unknown format names. Draft 7 uses its vocabulary's format policy when this option is omitted.

Standalone compilation accepts boolean schemas; MCP tool registration still requires schema maps. The compiled value should be treated as opaque.

validate_compiled(instance, compiled)

@spec validate_compiled(term(), Snodo.Schema.Validator.JSV.Compiled.t()) ::
  :ok | {:error, term()}

Validates an instance using an immutable root; the original value is never replaced.