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

contract

package
v0.36.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: 16 Imported by: 0

Documentation

Overview

Package contract enforces the funcd contract type profile (ADR-0058): the bounded, language-agnostic subset of JSON Schema 2020-12 that a function's I/O contract may use — "the def" (the discipline JTD and WIT apply). A contract is GENERATED from the author's code type (typia / pydantic) and then run through Check before it is shipped or embedded, so a contract can never advertise a shape a peer runtime (or the registry) can't honor.

Supported: scalars (string + format date-time/uuid/email/uri, pattern, length; integer + format int32/int64; number; boolean), string enums, CLOSED objects (additionalProperties:false), arrays, string-keyed maps (additionalProperties:<type>, no properties), DISCRIMINATED unions (oneOf + discriminator), optional/nullable, and the explicit `Json` form (the empty schema {} — arbitrary JSON).

Forbidden (Check returns a fault.Invalid naming the construct): OPEN records (additionalProperties true/absent on a record), NON-DISCRIMINATED oneOf/anyOf/allOf, not, if/then/else, external/recursive $ref, and recursive types (which require $ref).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(schemaJSON []byte) error

Check reports whether a JSON Schema (a generated function I/O contract) lies within the funcd profile. It returns nil when every construct is supported, or a fault.Invalid whose message names the first unsupported construct and its JSON-pointer-ish path. A nil/empty input is treated as the `Json` (any) form — valid. A side in the profile must also compile with funcd's rules (ADR-0301 Decision 4), so a pattern no check could run fails here.

Types

type Detail added in v0.36.0

type Detail struct {
	Path    string `json:"path"`    // RFC 6901 JSON Pointer to the failing value; "" is the root
	Message string `json:"message"` // for people; the wording is not part of the contract
}

Detail is one entry of a 422 or 500 body's details (ADR-0301 Decision 3).

type Option added in v0.36.0

type Option func(*options)

Option configures Compile.

func WithCallBounds added in v0.36.0

func WithCallBounds(budget time.Duration, maxDigits int) Option

WithCallBounds bounds each Validate call (ADR-0303 Decision 6): pattern matching stops after budget, and each pattern asked later fails its value, with a root detail naming the budget; a number with more than maxDigits digits, or an exponent over maxDigits in magnitude, fails at its path before the engine reads any number. Zero leaves a bound off, as ADR-0301's default is.

func WithMatchTimeout added in v0.36.0

func WithMatchTimeout(d time.Duration) Option

WithMatchTimeout bounds each pattern match (default 100 ms). A match that runs longer fails the value.

type TypeField added in v0.2.4

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

TypeField captures JSON Schema's `type`, which is a string OR an array of strings. The type generator (pkg/sdk) decodes `type` with it too, so it reads every contract the gate accepts.

func (TypeField) Nullable added in v0.2.4

func (t TypeField) Nullable() bool

Nullable reports the ADR-0058 nullable form: a type list that holds "null" beside another type.

func (TypeField) Primary added in v0.2.4

func (t TypeField) Primary() string

Primary returns the first non-"null" type, or "" when no type keyword is present.

func (*TypeField) UnmarshalJSON added in v0.2.4

func (t *TypeField) UnmarshalJSON(b []byte) error

type Validator added in v0.36.0

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

Validator checks JSON documents against one compiled contract side. It is safe for concurrent use.

func Compile added in v0.36.0

func Compile(schemaJSON []byte, opts ...Option) (*Validator, error)

Compile compiles one contract side as JSON Schema 2020-12 with funcd's rules (Decisions 1 and 2). An empty schemaJSON compiles to a Validator that accepts every document (ADR-0058's Json form). A side that does not compile returns a fault.Invalid naming its schema location.

func (*Validator) AcceptsAll added in v0.36.0

func (v *Validator) AcceptsAll() bool

AcceptsAll reports whether the side accepts every document, as ADR-0058's Json form ({} or true) does, so a check need not read the document (ADR-0303).

func (*Validator) Validate added in v0.36.0

func (v *Validator) Validate(doc []byte) ([]Detail, error)

Validate returns every error of doc (Decision 3), or nil when doc is valid. Numbers are compared exactly, never through float64. It returns a fault.Invalid when doc is not JSON. It does no I/O, and the match timeout bounds each pattern match (Decision 2), so it takes no context.

Jump to

Keyboard shortcuts

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