schema

package
v0.0.0-...-ecee8c2 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 22 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package schema generates JSON Schema from Go types, compiles and validates instances, and produces RFC 8785 canonical JSON for content-addressing.

Generation maps []byte and named types over []byte to nullable strings with contentEncoding "base64". json.RawMessage accepts any JSON value including null. Fixed-length [N]byte arrays remain JSON arrays of integers.

Struct schemas follow encoding/json object keys for promoted fields, nested tagged embeds and exported anonymous non-struct fields; a struct that embeds json.RawMessage (value or pointer) admits any JSON value at that schema node; unsupported embeds such as channels fail at generation.

Untrusted JSON is bounded: nesting depth at most 10000; number literals at most 1000 mantissa digits and exponent magnitude at most 8192; canonicalization still enforces exact IEEE-754 doubles.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Canonicalize

func Canonicalize(text []byte) ([]byte, error)

Canonicalize returns the RFC 8785 canonical form of text.

func ErrLocation

func ErrLocation(err error) string

ErrLocation returns the instance JSON Pointer from a platform error, if any.

func Marshal

func Marshal(v any) ([]byte, error)

Marshal JSON-encodes v and returns its canonical form.

func ReferenceFromErr

func ReferenceFromErr(err error) string

ReferenceFromErr returns an external reference named in a compilation error.

Types

type Compiled

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

Compiled is a schema ready to validate instances against.

func Compile

func Compile(raw json.RawMessage) (*Compiled, error)

Compile parses and compiles a single schema with no external references.

func (*Compiled) Validate

func (c *Compiled) Validate(instance json.RawMessage) error

Validate checks instance against the compiled schema.

type Schema

type Schema struct {
	Raw json.RawMessage
}

Schema is a JSON Schema document, usually generated from a Go type.

func For

func For[T any]() (*Schema, error)

For generates a JSON Schema for T.

func ForType

func ForType(t reflect.Type) (*Schema, error)

ForType generates a JSON Schema for a Go type.

type SchemaViolation

type SchemaViolation struct {
	SchemaLocation string `json:"schema_location"`
	Keyword        string `json:"keyword"`
	Message        string `json:"message"`
}

SchemaViolation is a metaschema failure while compiling a schema.

func SchemaViolations

func SchemaViolations(err error) []SchemaViolation

SchemaViolations returns compilation violations from err.

type Set

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

Set is the closed world of schemas a document may refer to.

func NewSet

func NewSet() *Set

NewSet returns an empty schema set.

func (*Set) Add

func (s *Set) Add(id string, raw json.RawMessage) error

Add registers a schema by id.

func (*Set) Compile

func (s *Set) Compile(id string) (*Compiled, error)

Compile compiles the schema id from the set.

type Violation

type Violation struct {
	Location string `json:"location"`
	Keyword  string `json:"keyword"`
	Message  string `json:"message"`
}

Violation is one schema or instance constraint failure.

func Violations

func Violations(err error) []Violation

Violations returns instance validation violations from err.

Jump to

Keyboard shortcuts

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