jsonschema

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package jsonschema validates JSON payloads against JSON Schema documents.

It is a separate package so that the JSON Schema compiler lands only in the graph of a service that actually validates JSON, and so that the protobuf runtime lands only in the graph of one that validates protobuf. The registry itself needs neither: to a store, a schema is bytes.

Index

Constants

This section is empty.

Variables

View Source
var ErrExternalReference = errors.NewSentinel("schema.jsonschema.external_reference",
	"schema: a schema may not reference anything outside itself")

ErrExternalReference is returned when a schema refers to something outside itself.

The compiler's default loader resolves a file: URL by reading local disk, so a published schema of {"$ref":"file:///etc/passwd"} makes validation depend on the filesystem of whichever replica handled the message. Two replicas then disagree about the same message, which is exactly the non-reproducibility the registry's immutability rule exists to prevent — and a schema is attacker-influenced input wherever publishing is less trusted than validating.

Note that this sentinel does not survive to the caller: the third-party compiler flattens a loader error to text, so the error a caller receives is schema.ErrSchemaUnusable carrying this one's message. It is exported so the reason is nameable and greppable, not so it can be matched with errors.Is.

Functions

This section is empty.

Types

type Checker

type Checker struct{}

Checker is the no-op compatibility checker for JSON Schema.

Real JSON Schema compatibility is a genuine problem — a widened enum is backward compatible, a narrowed one is not, and additionalProperties flips the answer for both — and nothing in the estate has evolved a schema yet, so choosing a model now would be a guess about which kind of break will matter. This exists so the seam is live code rather than a comment.

func (Checker) Check

Check accepts everything.

func (Checker) Language

func (Checker) Language() schema.Language

Language reports schema.JSONSchema.

type Validator

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

Validator checks JSON payloads against JSON Schema documents.

Compiled schemas are cached, keyed by identifier AND by a digest of the bytes. The identifier alone would be wrong: immutability is a property of one store, while a Validator is a process-wide singleton fed by any Resolver — an embedded register and a network client in the same process, or a test fake. Keyed on the URN alone, the first bytes seen would validate every later payload for the life of the process, with no error path and no way to notice.

func New

func New() *Validator

New returns a validator with an empty cache.

func (*Validator) Cached

func (v *Validator) Cached() int

Cached reports how many compiled schemas the validator is holding.

It exists so the cache can be asserted rather than assumed. A cache that never populates behaves identically to one that does, from every other angle — which is exactly the kind of documented behaviour that quietly stops being true.

func (*Validator) Language

func (v *Validator) Language() schema.Language

Language reports schema.JSONSchema.

func (*Validator) Validate

func (v *Validator) Validate(_ context.Context, s schema.Schema, payload []byte) error

Validate returns schema.ErrInvalidPayload when the payload does not conform to the schema.

A schema that will not compile is schema.ErrSchemaUnusable, not ErrInvalidPayload. The two have different owners: one is the producer's problem and the other is ours, and a schema.Guard fails closed on the first and open on the second. Reporting them alike is how a mis-published schema rejects every message on a topic while the fail-open counter reads zero.

Jump to

Keyboard shortcuts

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