schema

package module
v0.0.0-...-e209eb6 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package schema provides JSON Schema generation from Go types.

It wraps github.com/invopop/jsonschema to produce standard JSON Schema 2020-12 documents from struct tags, and exposes the result as a plain map[string]any suitable for tool definitions, MCP, and LLM APIs.

Usage:

type SearchInput struct {
    Query    string `json:"query"    jsonschema:"required,description=Search query text"`
    Platform string `json:"platform" jsonschema:"enum=youtube,enum=tiktok,enum=instagram"`
    Limit    int    `json:"limit"    jsonschema:"minimum=1,maximum=100"`
}

s := schema.Generate[SearchInput]()
// s is a map[string]any representing the JSON Schema for SearchInput.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSON

type JSON = map[string]any

JSON is a standard JSON Schema document represented as a map. Using map[string]any keeps the schema format-agnostic and easily serializable to any wire format (OpenAI, Anthropic, MCP, etc.).

func From

func From(t reflect.Type, opts ...Option) JSON

From creates a JSON Schema from a reflect.Type. Use this when the type is not known at compile time.

func Generate

func Generate[T any](opts ...Option) JSON

Generate creates a JSON Schema from a Go type using struct tags. The type parameter T should be a struct with json and optional jsonschema tags.

schema.Generate[SearchInput]()
schema.Generate[SearchInput](schema.WithTitle("Search"), schema.WithDescription("..."))

type Option

type Option func(*config)

Option configures JSON Schema generation.

func WithAdditionalProperties

func WithAdditionalProperties() Option

WithAdditionalProperties allows additional properties in the generated schema. Default is strict (no additional properties).

func WithDefinitions

func WithDefinitions() Option

WithDefinitions keeps $defs and uses $ref for nested types instead of inlining all definitions. Default is to inline.

func WithDescription

func WithDescription(desc string) Option

WithDescription overrides the root schema description.

func WithTitle

func WithTitle(title string) Option

WithTitle overrides the root schema title.

type ValidationError

type ValidationError struct {
	// Path is the JSON pointer to the invalid field (e.g., "/query", "/items/0").
	Path string `json:"path"`
	// Message describes what's wrong.
	Message string `json:"message"`
}

ValidationError describes a single validation failure.

func (ValidationError) Error

func (e ValidationError) Error() string

type ValidationResult

type ValidationResult struct {
	Valid  bool              `json:"valid"`
	Errors []ValidationError `json:"errors,omitempty"`
}

ValidationResult holds the outcome of validating a value against a schema.

func Validate

func Validate(s JSON, value any) ValidationResult

Validate checks a value against a JSON Schema and returns validation results. The schema should be a JSON Schema document (as returned by Generate or From). The value can be any Go value that is JSON-serializable.

result := schema.Validate(mySchema, input)
if !result.Valid {
    for _, err := range result.Errors {
        log.Printf("validation error at %s: %s", err.Path, err.Message)
    }
}

Jump to

Keyboard shortcuts

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