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 ¶
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.
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.
var ErrUnsupported = errors.New("unsupported JSON Schema")
ErrUnsupported reports a schema that uses something this validator does not implement.
Functions ¶
Types ¶
type Compiler ¶
type Compiler struct {
// contains filtered or unexported fields
}
Compiler compiles the schemas of one document, sharing what they reference.
func NewCompiler ¶
NewCompiler returns a compiler for the document root, a decoded JSON value (from Decode or from encoding/json/v2: numbers as Number or float64).
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.
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. |