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 ¶
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 ¶
From creates a JSON Schema from a reflect.Type. Use this when the type is not known at compile time.
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 ¶
WithDescription overrides the root schema description.
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)
}
}