Help us shape the future of Go! Take the 2026 Go Developer Survey

schemainfer

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

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

View Source
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

View Source
var ErrBadSchema = errors.New("the schema is not a valid JSON Schema")

ErrBadSchema is returned when a stored schema cannot be compiled.

Functions

func Merge

func Merge(existing Schema, sample any) (Schema, []Change)

Merge widens existing with what sample shows and reports each difference. A schema marked x-flowi-source openapi (imported from a spec) or user (edited by hand) is returned unchanged: inference must not rewrite what a person or a spec said.

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.

const (
	DriftNew     DriftKind = "new"     // a field the schema does not know
	DriftMissing DriftKind = "missing" // a required field that is gone
	DriftChanged DriftKind = "changed" // a field of another type
)

Kinds of drift.

type Schema

type Schema = map[string]any

Schema is a JSON Schema object as decoded JSON.

func Infer

func Infer(sample any) Schema

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

type Violation struct {
	Path    string // $.user.tags[0]
	Message string
}

Violation is one place a sample breaks a schema.

func Validate

func Validate(schema Schema, sample any) ([]Violation, error)

Validate checks sample (decoded JSON) against schema. Formats are asserted (a date-time that is not one is a violation). A schema that does not compile is ErrBadSchema. Nothing is cached: schemas are small and rarely checked more than once per send.

Jump to

Keyboard shortcuts

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