silk.json.schema.

Compile JSON Schema resources and validate immutable JSON or TOML-derived values.

Details#

Supports drafts 4, 6, 7, 2019-09, and 2020-12. Compilation requests external documents through the caller's SchemaResolver; this module performs no filesystem or network I/O. Parse the root with Json.parseExact or SchemaValue.parseToml and keep it alive while using the compiled schema. Formats are annotations unless assertion is explicitly enabled.

Import as Schema with import silk.json.schema { Schema }.

Public declarations: 8.

Draft#

silk
pub enum Draft

The JSON Schema dialect used when a resource has no $schema declaration.

Draft4#

silk
Draft4

JSON Schema draft 4.

Draft6#

silk
Draft6

JSON Schema draft 6.

Draft7#

silk
Draft7

JSON Schema draft 7.

Draft201909#

silk
Draft201909

JSON Schema draft 2019-09.

Draft202012#

silk
Draft202012

JSON Schema draft 2020-12.

Options#

silk
pub struct Options

Compilation policy, including the fallback dialect and optional format and content assertions.

Field draft#

silk
pub draft: Draft

Dialect for a root resource without $schema.

Field assertFormats#

silk
pub assertFormats: bool

Assert known formats; unknown names remain annotations unless the format-assertion vocabulary is used.

Field assertContent#

silk
pub assertContent: bool

In draft 7, assert base64 content encoding and application/json media type. Disabled by default. Unknown encodings and media types remain annotations; in other drafts all content keywords remain annotations, regardless of this option.

Implementation Copy for Options#

silk
impl Copy for Options

SchemaReason#

silk
pub enum SchemaReason

The kind of schema, dialect, or reference error; instance failures use Output instead.

InvalidSchema#

silk
InvalidSchema

A recognized keyword has an invalid schema value.

InvalidInstance#

silk
InvalidInstance

The instance tree contains a non-finite number, which is outside the JSON data model.

UnknownDialect#

silk
UnknownDialect

The declared dialect cannot be resolved to a supported dialect.

UnknownVocabulary#

silk
UnknownVocabulary

A required vocabulary is not implemented.

InvalidUri#

silk
InvalidUri

A schema identifier or reference is not a valid URI reference.

DuplicateIdentifier#

silk
DuplicateIdentifier

Two distinct schemas declare the same identifier or anchor.

UnresolvedReference#

silk
UnresolvedReference

No supplied schema resource or anchor resolves the reference.

InvalidReference#

silk
InvalidReference

A reference does not designate a schema.

NonterminatingReference#

silk
NonterminatingReference

A reference cycle applies a schema to the same instance without progress.

SchemaError#

silk
pub struct SchemaError

A schema compilation or evaluation failure, distinct from an invalid instance.

Field reason#

silk
pub reason: SchemaReason

Stable category of the failure.

Field keywordLocation#

silk
pub keywordLocation: String

JSON Pointer to the problematic schema keyword.

Field detail#

silk
pub detail: String

Human-readable description, including an unresolved URI when relevant.

Diagnostic#

silk
pub struct Diagnostic

One failed keyword evaluation, with the locations used by JSON Schema basic output.

Field keywordLocation#

silk
pub keywordLocation: String

Evaluation-path JSON Pointer to the failed keyword.

Field absoluteKeywordLocation#

silk
pub absoluteKeywordLocation: String

Absolute URI identifying the failed schema keyword.

Field instanceLocation#

silk
pub instanceLocation: String

JSON Pointer identifying the instance value that failed.

Field error#

silk
pub error: String

Human-readable validation error.

Output#

silk
pub struct Output

A validation result. Schema.toJson converts it to the standard basic output representation.

Field valid#

silk
pub valid: bool

Whether the complete instance satisfies the schema.

Field errors#

silk
pub errors: List<Diagnostic>

Failed keyword evaluations; empty when valid is true.

CompiledSchema#

silk
pub struct CompiledSchema<'a>

An indexed schema that borrows its root and owns all resolved external document trees.

Gotchas#

Keep only the root tree alive and immutable for this value's lifetime. The resolver and its inputs may be dropped after compilation. Validation never requires a resolver.

Schema#

silk
pub struct Schema

Operations for compiling JSON Schema resources and validating instance trees.

Implementation Schema#

silk
impl Schema

Associated function options#

silk
pub fn options(draft: Draft) -> Options

Returns options for draft, with format and content assertions disabled.

Associated function compile#

silk
pub effect fn compile<'a>(schema: &'a Value, baseUri: string,
    options: Options) -> CompiledSchema<'a>
    ! SchemaError | SchemaResolutionError | RegexError | OutOfMemoryError
    ? &mut SchemaResolver | &mut Allocator

Validates and indexes a root schema, resolving external documents through SchemaResolver.

Details#

baseUri is the absolute retrieval URI of the root. An empty URI gives the root an anonymous local base. $schema overrides options.draft. The caller must provide a SchemaResolver, including for custom metaschemas. Each retrieval URI is requested once; resolved trees are owned by the result, and the root remains borrowed.

Errors#

  • SchemaError for an invalid schema, dialect, vocabulary, identifier, or reference.

  • RegexError for an invalid regular expression in a schema.

  • SchemaResolutionError for operational resolver failures, propagated unchanged.

  • OutOfMemoryError if schema indexing cannot allocate storage.

Associated function validate#

silk
pub effect fn validate(compiled: &CompiledSchema, instance: &Value) -> Output
    ! SchemaError | RegexError | OutOfMemoryError ? &mut Allocator

Applies a compiled schema and returns JSON Schema basic output.

Errors#

  • SchemaError.NonterminatingReference for a reference cycle without instance progress.

  • SchemaError.InvalidInstance if a manually constructed instance contains a non-finite number.

  • RegexError if a regular expression cannot be evaluated.

  • OutOfMemoryError if evaluation or diagnostics cannot allocate storage.

An invalid instance is not an error effect: the returned Output.valid is false.

Associated function toJson#

silk
pub effect fn toJson(output: &Output) -> Value ! OutOfMemoryError ? &mut Allocator

Converts a validation result to a JSON Schema standard basic output tree.

Details#

Includes root and error-unit validity and JSON Pointer locations. Successful output omits errors; failed output contains the keyword diagnostics. Write the tree with Json.write.

Errors#

  • OutOfMemoryError if the JSON output tree cannot be allocated.

Category: Data formats.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page