jsonschemautil

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 36 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

Index

Constants

This section is empty.

Variables

View Source
var ErrUnresolvable = errors.New("unresolvable reference")

Reported, wrapped in a *SchemaError, for a reference to a document that is neither in the schema, bundled, given with WithResource nor served by a loader, or to a place or anchor its document lacks

View Source
var ErrUnsupportedURI = errors.New("unsupported URI")

Wrapped by a Loader for a URI it does not serve, so the next loader is asked

Functions

func Validate

func Validate(value, schema any, opts ...Option) error

Most relevant failure of the value against the schema as a *ValidationError, a *SchemaError when the schema does not compile, nil when the value conforms

Types

type Draft

type Draft int

JSON Schema dialect, named by the year or number of its draft

const (
	// Dialect of draft 3
	Draft3 Draft = 3
	// Dialect of draft 4
	Draft4 Draft = 4
	// Dialect of draft 6
	Draft6 Draft = 6
	// Dialect of draft 7
	Draft7 Draft = 7
	// Dialect of draft 2019-09
	Draft201909 Draft = 2019
	// Dialect of draft 2020-12, the default
	Draft202012 Draft = 2020
)

type Loader

type Loader func(ctx context.Context, uri string) (any, error)

Source of documents a schema refers to by URI: the JSON document at the URI, decoded; an error wrapping ErrUnsupportedURI hands the URI to the next loader

func DataLoader

func DataLoader() Loader

Loader decoding data URIs, which carry the document itself, percent-encoded or in base64

func FileLoader

func FileLoader(fsys fs.FS) Loader

Loader reading file URIs from fsys, the path of the URI without its leading slash naming the file there; a URI naming another host is not served

func HTTPLoader

func HTTPLoader(client *http.Client) Loader

Loader fetching http and https URIs with a GET through client, or when client is nil through a shared client that honours the proxy settings and the address family preference

type Option

type Option func(*config)

Setting for Compile, CompileContext and Validate

func AssertContent

func AssertContent() Option

contentEncoding, contentMediaType and contentSchema asserted on string values: base16, base32 and base64 encodings, JSON media types and the schema of the decoded document

func AssertFormats

func AssertFormats() Option

format asserted on string values for every format the package knows instead of only noted, as JSON Schema leaves it by default

func WithBaseURI

func WithBaseURI(uri string) Option

URI the root schema was retrieved from: the base its relative $id and references resolve against

func WithDraft

func WithDraft(d Draft) Option

Dialect for a schema whose $schema names none the package knows, Draft202012 when not given

func WithFormat

func WithFormat(name string, valid func(string) bool) Option

Check asserted for the named format on string values, taking the place of the built-in one

func WithLoader

func WithLoader(l Loader) Option

Loader asked, after those given before it, for referenced documents the schema does not contain and no WithResource gives; without a loader such references are errors and nothing is fetched

func WithMatchTimeout

func WithMatchTimeout(d time.Duration) Option

Time one pattern match may take, and a tenth of what all matches of one validation may take together, before the validation stops as failed; 100ms when not given, zero or less lets matches run unbounded

func WithResource

func WithResource(uri string, doc any) Option

Document a reference to uri reaches, so no loader is asked for it; repeated for several documents

type Schema

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

JSON Schema compiled once, every reference resolved: immutable and safe for concurrent use

func Compile

func Compile(schema any, opts ...Option) (*Schema, error)

Schema compiled from a decoded JSON value (*jsonutil.Object, map[string]any, []any, numbers, strings, booleans, nil) after checking it against the metaschema its $schema names; a *SchemaError when it is not a valid schema or a reference cannot be resolved

func CompileContext

func CompileContext(ctx context.Context, schema any, opts ...Option) (*Schema, error)

Schema compiled as by Compile, ctx passed to the loaders that fetch referenced documents

func (*Schema) Errors

func (s *Schema) Errors(value any) []*ValidationError

Every failure of the value in the order the keywords of the schema produced them, those of anyOf and oneOf alternatives as Causes; nil when the value conforms; a validation that cannot finish (a pattern match out of time, a schema whose work explodes) gives only the error saying why

func (*Schema) Valid

func (s *Schema) Valid(value any) bool

Whether the value conforms, stopping at the first failure without describing it

func (*Schema) Validate

func (s *Schema) Validate(value any) error

Most relevant failure of the value, chosen by BestMatch, as a *ValidationError; nil when the value conforms

type SchemaError

type SchemaError struct {
	// Where the problem is: a URI with a JSON pointer fragment written without percent-encoding, # alone for the root of the schema
	Location string
	// What is wrong
	Message string
	// The metaschema rule the schema breaks, its InstancePath leading into the schema; nil for other problems
	Violation *ValidationError
	// Underlying cause, such as ErrUnresolvable, a loader failure or a pattern syntax error; nil when there is none
	Err error
}

Schema that cannot be compiled: it breaks its metaschema, a reference cannot be resolved, or a keyword holds a value it cannot use

func (*SchemaError) Error

func (e *SchemaError) Error() string

The problem with its location in the schema

func (*SchemaError) Unwrap

func (e *SchemaError) Unwrap() error

The underlying cause; the metaschema violation is kept out of the chain so it is never taken for a failure of the value

type ValidationError

type ValidationError struct {
	// What is wrong, in plain words with values quoted as JSON
	Message string
	// Keyword that failed, empty for a false schema or a validation that cannot finish for a reason other than a pattern match running out of time
	Keyword string
	// Steps from the root of the value to the failing part: property names as string, array positions as int
	InstancePath []any
	// JSON pointer from the root schema to the failing keyword along the path evaluation took, references included
	SchemaPath string
	// Absolute location of the failing keyword: the URI of its schema resource with a JSON pointer fragment, written as the pointer reads, without percent-encoding
	SchemaURI string
	// Failures of the subschemas behind this one, for anyOf, oneOf and the keywords that summarize several failures
	Causes []*ValidationError
	// contains filtered or unexported fields
}

Failure of a value against a schema: what is wrong, where in the value, and which keyword at which place in the schema

func BestMatch

func BestMatch(errs []*ValidationError) *ValidationError

Most relevant of a list of errors, nil for an empty list: among the errors, the one highest in the value, not from anyOf or oneOf, whose value has a type its schema rules out, the first of equals; then into its causes for as long as one of them is more relevant than the rest, there preferring the deepest, from anyOf or oneOf, whose value has the type its schema expects

func (*ValidationError) Error

func (e *ValidationError) Error() string

The message, preceded by where in the value it applies and followed by the schema keyword that failed

func (*ValidationError) Location

func (e *ValidationError) Location() string

JSON pointer of the failing part of the value, empty for the value itself

Jump to

Keyboard shortcuts

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