jsonschema

package
v0.30.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package jsonschema validates JSON documents somebody else wrote against a JSON Schema 2020-12 they publish — a webhook's payload, an OpenAPI request body, another program's configuration (#882, ADR 0210) — with the standard library alone.

NewValidator compiles the core, applicator and validation vocabularies: "$ref" within the document and to the resources Config registers by URI — a remote reference is refused, never fetched —, "$id", "$anchor", "$defs", the combinators, the conditionals, the array and object applicators, and every validation keyword, numbers compared exactly. "format" is an annotation unless Config.AssertFormats names it (date-time, uuid, email). Out of scope, each refused by name: drafts before 2020-12, "$dynamicRef" and "$dynamicAnchor", "$vocabulary", the unevaluated and the content vocabularies; a "pattern" Go's regexp does not read is refused when the schema is compiled.

Validate reads a document through strictjson's bounds — a byte ceiling, one value, no duplicated member name — and returns a validation.ReportValue: each violation located in the validation package's path grammar ("items[2].name"), named by its keyword, and never quoting the document. A compiled Validator is immutable and reused.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Resources are schema documents a "$ref" may name, by URI: each is a
	// resource at that URI, and at its "$id" when it has one. A reference to
	// any other URI is refused; nothing is fetched.
	Resources map[string][]byte
	// AssertFormats are the formats asserted rather than annotated:
	// "date-time" (RFC 3339), "uuid" and "email" (an addr-spec). Naming
	// another is refused.
	AssertFormats []string
}

Config configures NewValidator. The zero Config compiles a self-contained schema with every format an annotation.

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator is a compiled JSON Schema 2020-12: immutable, safe for concurrent use, reused for every document it validates.

func NewValidator

func NewValidator(schema []byte, cfg Config) (*Validator, error)

NewValidator compiles schema, a JSON Schema 2020-12 document, under cfg. It refuses with SchemaInvalid a schema that is not exactly one JSON value or whose keyword has the wrong shape, KeywordRefused one using a keyword out of scope — "$dynamicRef" and "$dynamicAnchor", "$vocabulary", "unevaluatedItems" and "unevaluatedProperties", the content vocabulary — or another draft's "$schema", RefUnresolved a "$ref" naming neither the document nor a registered resource, and FormatUnknown a format cfg asserts that this package cannot check.

func (*Validator) Validate

func (v *Validator) Validate(doc []byte, maxBytes int64) (validation.ReportValue, error)

Validate reads doc through strictjson's bounds — at most maxBytes, one value, no duplicated member name — and returns the violations of the schema, located in the validation package's path grammar ("items[2].name"). A message never quotes the document. An empty report means doc is valid; an error means doc was not read: strictjson's refusal, or ReferenceLoop for a "$ref" cycle that consumes nothing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL