schema

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package schema validates JSON values against the JSON Schema documents the typed operations of the event handler generate from struct tags (through github.com/swaggest/jsonschema-go): a deliberately small JSON Schema 2020-12 validator.

A general validator compiles the meta-schemas of every draft when its package initializes, which costs milliseconds of every cold start; the event handler only needs the keywords its reflector writes. Compile rejects any other keyword, so that a schema is never half enforced.

Supported: type (with null), enum, const, format (date-time, date, time, duration, email, hostname, ipv4, ipv6, uri, uri-reference, iri, iri-reference, uuid, regex; int32, int64, float, double, byte, binary and password are annotations), $ref to a JSON pointer of the same document, properties, required, additionalProperties, minProperties, maxProperties, items, minItems, maxItems, uniqueItems, minLength, maxLength (in runes), pattern (RE2, unanchored), minimum, maximum, exclusiveMinimum, exclusiveMaximum, multipleOf, allOf, anyOf, oneOf, boolean schemas. Annotations (title, description, default, examples, deprecated...) and x- extensions are ignored.

The order and the shape of the failures follow github.com/santhosh-tekuri/jsonschema/v6, which the event handler used before: type, const, enum and format stop the evaluation of their schema; the other keywords all report.

Index

Constants

This section is empty.

Variables

View Source
var ErrCycle = fmt.Errorf("%w: reference cycle", ErrUnsupported)

ErrCycle reports references that lead back to themselves without going down the instance, which no value can satisfy or fail.

View Source
var ErrNumberRange = errors.New("number out of range")

ErrNumberRange reports a JSON number no Go field could hold, which exact arithmetic would spend an unbounded time on (1e999999 takes big.Rat tens of milliseconds): a request is not allowed to make the function work that hard for a value it must reject anyway.

View Source
var ErrUnsupported = errors.New("unsupported JSON Schema")

ErrUnsupported reports a schema that uses something this validator does not implement.

Functions

func Decode

func Decode(data []byte) (any, error)

Decode decodes JSON into the values Validate reads: nil, bool, Number, string, []any and map[string]any. As encoding/json, it accepts duplicate names (the last one wins) and replaces invalid UTF-8.

Types

type Compiler

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

Compiler compiles the schemas of one document, sharing what they reference.

func NewCompiler

func NewCompiler(root any) *Compiler

NewCompiler returns a compiler for the document root, a decoded JSON value (from Decode or from encoding/json/v2: numbers as Number or float64).

func (*Compiler) Compile

func (c *Compiler) Compile(pointer string) (*Schema, error)

Compile compiles the schema at pointer, a URI fragment such as "#/$defs/request" ("#" is the whole document).

type Failure

type Failure struct {
	// Loc locates the failing value in the instance: member names, and
	// array indexes as ints.
	Loc []any
	// Keyword is the keyword that failed: "type", "required",
	// "minLength"... "false" for a false schema, "anyOf" or "oneOf" when no
	// branch says more.
	Keyword string
	// Names are the missing members of required, or the forbidden ones of
	// additionalProperties.
	Names []string
	// Got and Want are the instance's type and the types allowed, for type.
	Got  string
	Want []string
	// Format is the format that failed.
	Format string
	// Msg explains the failure.
	Msg string
}

Failure is one way an instance breaks a schema.

type Number

type Number string

Number is a JSON number, kept as its text so that no precision is lost.

type Schema

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

Schema is a compiled schema. It is safe for concurrent use.

func (*Schema) Validate

func (s *Schema) Validate(v any) []Failure

Validate validates v and returns its failures, none when it conforms.

Directories

Path Synopsis
Package schematest generates JSON Schema documents of the subset the schema package implements, and JSON instances, from fuzzer bytes, so that tests can compare the validator with a reference implementation.
Package schematest generates JSON Schema documents of the subset the schema package implements, and JSON instances, from fuzzer bytes, so that tests can compare the validator with a reference implementation.

Jump to

Keyboard shortcuts

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