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#
pub enum DraftThe JSON Schema dialect used when a resource has no $schema declaration.
Draft4#
Draft4JSON Schema draft 4.
Draft6#
Draft6JSON Schema draft 6.
Draft7#
Draft7JSON Schema draft 7.
Draft201909#
Draft201909JSON Schema draft 2019-09.
Draft202012#
Draft202012JSON Schema draft 2020-12.
Options#
pub struct OptionsCompilation policy, including the fallback dialect and optional format and content assertions.
Field draft#
pub draft: DraftDialect for a root resource without $schema.
Field assertFormats#
pub assertFormats: boolAssert known formats; unknown names remain annotations unless the format-assertion vocabulary is used.
Field assertContent#
pub assertContent: boolIn 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#
impl Copy for OptionsSchemaReason#
pub enum SchemaReasonThe kind of schema, dialect, or reference error; instance failures use Output instead.
InvalidSchema#
InvalidSchemaA recognized keyword has an invalid schema value.
InvalidInstance#
InvalidInstanceThe instance tree contains a non-finite number, which is outside the JSON data model.
UnknownDialect#
UnknownDialectThe declared dialect cannot be resolved to a supported dialect.
UnknownVocabulary#
UnknownVocabularyA required vocabulary is not implemented.
InvalidUri#
InvalidUriA schema identifier or reference is not a valid URI reference.
DuplicateIdentifier#
DuplicateIdentifierTwo distinct schemas declare the same identifier or anchor.
UnresolvedReference#
UnresolvedReferenceNo supplied schema resource or anchor resolves the reference.
InvalidReference#
InvalidReferenceA reference does not designate a schema.
NonterminatingReference#
NonterminatingReferenceA reference cycle applies a schema to the same instance without progress.
SchemaError#
pub struct SchemaErrorA schema compilation or evaluation failure, distinct from an invalid instance.
Field reason#
pub reason: SchemaReasonStable category of the failure.
Field keywordLocation#
pub keywordLocation: StringJSON Pointer to the problematic schema keyword.
Field detail#
pub detail: StringHuman-readable description, including an unresolved URI when relevant.
Diagnostic#
pub struct DiagnosticOne failed keyword evaluation, with the locations used by JSON Schema basic output.
Field keywordLocation#
pub keywordLocation: StringEvaluation-path JSON Pointer to the failed keyword.
Field absoluteKeywordLocation#
pub absoluteKeywordLocation: StringAbsolute URI identifying the failed schema keyword.
Field instanceLocation#
pub instanceLocation: StringJSON Pointer identifying the instance value that failed.
Field error#
pub error: StringHuman-readable validation error.
Output#
pub struct OutputA validation result. Schema.toJson converts it to the standard basic output representation.
Field valid#
pub valid: boolWhether the complete instance satisfies the schema.
Field errors#
pub errors: List<Diagnostic>Failed keyword evaluations; empty when valid is true.
CompiledSchema#
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#
pub struct SchemaOperations for compiling JSON Schema resources and validating instance trees.
Implementation Schema#
impl SchemaAssociated function options#
pub fn options(draft: Draft) -> OptionsReturns options for draft, with format and content assertions disabled.
Associated function compile#
pub effect fn compile<'a>(schema: &'a Value, baseUri: string,
options: Options) -> CompiledSchema<'a>
! SchemaError | SchemaResolutionError | RegexError | OutOfMemoryError
? &mut SchemaResolver | &mut AllocatorValidates 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#
SchemaErrorfor an invalid schema, dialect, vocabulary, identifier, or reference.RegexErrorfor an invalid regular expression in a schema.SchemaResolutionErrorfor operational resolver failures, propagated unchanged.OutOfMemoryErrorif schema indexing cannot allocate storage.
Associated function validate#
pub effect fn validate(compiled: &CompiledSchema, instance: &Value) -> Output
! SchemaError | RegexError | OutOfMemoryError ? &mut AllocatorApplies a compiled schema and returns JSON Schema basic output.
Errors#
SchemaError.NonterminatingReferencefor a reference cycle without instance progress.SchemaError.InvalidInstanceif a manually constructed instance contains a non-finite number.RegexErrorif a regular expression cannot be evaluated.OutOfMemoryErrorif evaluation or diagnostics cannot allocate storage.
An invalid instance is not an error effect: the returned Output.valid is false.
Associated function toJson#
pub effect fn toJson(output: &Output) -> Value ! OutOfMemoryError ? &mut AllocatorConverts 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#
OutOfMemoryErrorif 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.