schema

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package schema compiles a JSON Schema document, validates JSON payloads against it, and builds a bounded, model-facing corrective message on a validation failure. See docs/history/schema.md for the contract.

Index

Constants

View Source
const MaxCorrectiveBytes = 1024

MaxCorrectiveBytes is the byte cap Corrective truncates its output to.

View Source
const MaxPayloadBytes = 64 << 10

MaxPayloadBytes is the admission cap on a Validate payload's byte length, checked before Validate unmarshals it. Symmetric to MaxSchemaBytes: the payload is this design's stated adversarial input (raw tool output, a raw model completion), so it gets the same fail-closed treatment as the schema document.

View Source
const MaxSchemaBytes = 16 << 10

MaxSchemaBytes is the admission cap on a schema document's byte length, checked before Compile parses it.

View Source
const MaxSchemaDepth = 32

MaxSchemaDepth is the admission cap on a schema document's object/array nesting depth, checked before Compile parses it.

Variables

View Source
var ErrAdmission = errors.New("schema: admission rejected")

ErrAdmission is Compile's error when a schema document fails an admission cap or carries an out-of-document $ref, before compilation runs. It is also Validate's error when payload exceeds MaxPayloadBytes, before any unmarshal attempt. Test with errors.Is.

View Source
var ErrCompile = errors.New("schema: compile failed")

ErrCompile is Compile's error when an admitted document is not a legal JSON Schema. Test with errors.Is.

View Source
var ErrMalformedPayload = errors.New("schema: payload is not valid JSON")

ErrMalformedPayload is Validate's error when payload is not parseable JSON. Test with errors.Is.

View Source
var ErrValidation = errors.New("schema: payload does not match schema")

ErrValidation is Validate's error when parsed JSON does not match the compiled schema. Test with errors.Is.

Functions

func Corrective

func Corrective(err error) string

Corrective builds a bounded, plain-text corrective message from a Validate error, safe to resend to a model. Returns "" for a nil err. Truncates at MaxCorrectiveBytes, never splitting a UTF-8 rune. A non-ErrValidation error (a malformed-payload error, for example) still renders a bounded message naming the failure kind, not the raw error text, so a caller-supplied payload byte stream can never inject arbitrary text into the corrective message. For ErrValidation, renders only the failing schema path and kind, never the payload's failing instance value.

Types

type Compiled

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

Compiled is a schema ready for repeated Validate calls. Built only through Compile.

func Compile

func Compile(schemaBytes []byte) (*Compiled, error)

Compile admits and compiles a JSON Schema document. Rejects a document over MaxSchemaBytes, over MaxSchemaDepth nested objects or arrays, or carrying a $ref outside the document, all before compilation runs. Returns ErrAdmission, wrapped with the specific reason, for any of the three. Calls the underlying compiler's UseLoader(nil) before compiling, disabling the library's default FileLoader, so any resolution attempt the admission scan cannot see fails closed instead of reading from local disk. This backstop's proven vector is a $schema keyword naming an external URI: the admission scan only inspects $ref, so a $schema pointing at a file:// URL reaches the compiler unblocked, and jsonschema/v6 v6.0.3's own meta-schema resolution calls Loader.Load on it. Verified against this library version: an in-document, "#"-prefixed $ref under an $id-rebased scope does not reach the loader at all, even when scope has shifted, because the library resolves such a $ref by matching the current scope's URL to a resource id already collected from this same document. Returns ErrCompile, wrapped with the compiler's own reason, when the document is not a legal JSON Schema.

func (*Compiled) Validate

func (c *Compiled) Validate(payload []byte) error

Validate validates payload as JSON against the compiled schema. Rejects payload over MaxPayloadBytes with ErrAdmission, before any unmarshal attempt; this mirrors Compile's byte cap on the schema document, since raw tool output and raw model completion text are this design's stated adversarial input. Returns ErrMalformedPayload when an admitted payload does not parse as JSON; the standard library's json decoder bounds its own recursion, so Validate adds no separate depth cap on payload. Returns ErrValidation, wrapped with the failing instance paths, when parsed JSON does not match the schema. Returns nil on a match. Safe for concurrent use: many goroutines may call Validate on one shared *Compiled value, matching this SDK's flow panel members, which run concurrently and may share one compiled schema across waves.

Jump to

Keyboard shortcuts

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