Documentation
¶
Overview ¶
Package schemainfer infers JSON Schema (2020-12) from sample bodies, widens a schema with later samples while reporting what changed, and validates samples against a schema. It works on decoded JSON values (maps, slices, strings, numbers, bools, nil), so it does not care where a body came from.
Index ¶
Constants ¶
const ( MaxDepth = 20 MaxNodes = 10000 )
Bounds on what Infer reads, so a huge or deeply nested body costs a bounded amount of work. A body past them gives a schema marked x-flowi-truncated.
Variables ¶
var ErrBadSchema = errors.New("the schema is not a valid JSON Schema")
ErrBadSchema is returned when a stored schema cannot be compiled.
Functions ¶
Types ¶
type Change ¶
type Change struct {
Path string // JSONPath-like place: $.user.email, $.items[]
Kind ChangeKind
Detail string
}
Change is one difference between a schema and what a sample showed.
type ChangeKind ¶
type ChangeKind string
ChangeKind says how a later sample changed a schema.
const ( OptionalField ChangeKind = "optional-field" // a required field was missing from the sample NewField ChangeKind = "new-field" // the sample has a field the schema did not know TypeWidened ChangeKind = "type-widened" // integer became number NowNullable ChangeKind = "now-nullable" // null joined the types )
The ways a schema changes when a sample does not fit it.
type Difference ¶
type Difference struct {
Path string // $.owner.email; list items are [*]
Kind DriftKind
Detail string
}
Difference is one place a response differs from its schema.
func Drift ¶
func Drift(schema Schema, sample any) []Difference
Drift compares a response with the schema stored for it and lists what changed: fields the schema does not know (new), required fields that are gone (missing), and fields of another type (changed). It is the other side of Validate: it does not judge the response, it says what moved, and it never widens the schema. A field the schema allows to be null or absent is not drift when it is. A whole number where the schema says number is not drift either. A place that appears many times (every item of a list) is listed once.
type DriftKind ¶
type DriftKind string
DriftKind says how a response differs from the schema learned from earlier ones.
type Schema ¶
Schema is a JSON Schema object as decoded JSON.
func Infer ¶
Infer describes one sample: types, nested properties (every present key is required), array items (the union of the elements) and string formats (date-time, uuid, email, uri). Numbers decoded as json.Number are integers when written without a fraction or exponent; a float64 counts as an integer when it is whole, because decoding cannot tell 132 from 132.0.
type Violation ¶
Violation is one place a sample breaks a schema.