outputvalidation

package
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package outputvalidation provides a pure, self-contained validator that checks a tool's structured output against its declared JSON Schema (draft 2020-12).

Contract:

  • Imports only stdlib, go.uber.org/zap, and github.com/santhosh-tekuri/jsonschema/v6.
  • MUST NOT import internal/server, internal/config, internal/storage, or mcp-go.
  • Safe for concurrent use (sync.Map cache, no shared mutable state).
  • Never blocks on the proxy's inability to compile a schema (FR-A9).
  • Never mutates the structured value passed to Validate.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Outcome

type Outcome int

Outcome indicates whether a structured output conforms to its schema.

const (
	// OutcomePass means the output is valid, there is no schema, or there is
	// nothing to validate. The caller forwards the payload unchanged.
	OutcomePass Outcome = iota

	// OutcomeViolate means the output failed a guard check or schema validation.
	OutcomeViolate
)

type Validator

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

Validator validates a tool's structured output against its declared JSON Schema, applying byte-size and nesting-depth guards first. Safe for concurrent use.

func New

func New(maxBytes, maxDepth int, logger *zap.Logger) *Validator

New creates a new Validator.

  • maxBytes <= 0 disables the byte-size guard.
  • maxDepth <= 0 disables the nesting-depth guard.
  • logger may be nil; if nil, zap.NewNop() is used.

func (*Validator) Validate

func (v *Validator) Validate(toolKey, schemaJSON string, structured any) Verdict

Validate checks structured against schemaJSON for the tool identified by toolKey.

Decision tree (in order):

  1. schemaJSON == "" → OutcomePass (FR-A7: no schema declared, no-op)
  2. structured == nil → OutcomePass (FR-A8: nothing to validate)
  3. Guards (byte size, nesting depth) — on breach, return OutcomeViolate immediately
  4. Schema compilation (cached) — uncompilable schemas log once and return OutcomePass (FR-A9)
  5. Schema validation — return OutcomeViolate on mismatch, OutcomePass on success

Validate MUST NOT mutate structured.

type Verdict

type Verdict struct {
	Outcome  Outcome
	Reason   string // human-readable violation detail; empty on pass
	GuardHit string // "" | "max_bytes" | "max_depth"
}

Verdict is the result returned by Validator.Validate. It is never persisted; it is purely transient.

func (Verdict) IsViolation

func (v Verdict) IsViolation() bool

IsViolation reports whether the verdict represents a violation.

Jump to

Keyboard shortcuts

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