schema

package
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package schema provides utilities for generating JSON Schema from Go structs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleArgs

func HandleArgs[T any](arguments any) (*T, error)

HandleArgs is a helper function that handles parsing and validating tool arguments into a strongly-typed struct.

func HandleArgsWithSchema added in v1.2.0

func HandleArgsWithSchema[T any](arguments any, schemaMap map[string]interface{}) (*T, error)

Enhanced HandleArgs to support more complex validations

func ValidateAndConvertArgs added in v1.2.0

func ValidateAndConvertArgs(schemaMap map[string]interface{}, args map[string]interface{}, paramType reflect.Type) (interface{}, error)

ValidateAndConvertArgs validates arguments against a schema and converts them to the appropriate type based on reflection target type. This is a more general version than HandleArgsWithSchema that works with any target Go type.

func ValidateConstraints added in v1.2.0

func ValidateConstraints(v *Validator, fieldName string, value interface{}, schema map[string]interface{})

ValidateConstraints validates a value against constraint properties in a schema

func ValidateStruct added in v1.2.0

func ValidateStruct(data interface{}) error

ValidateStruct validates a struct against its schema definition tags.

func ValidateType added in v1.2.0

func ValidateType(value interface{}, expectedType string) bool

ValidateType checks if a value matches the expected JSON Schema type. This is a non-method version of validateType for shared usage.

func ValidateValueAgainstSchema added in v1.2.0

func ValidateValueAgainstSchema(v *Validator, fieldName string, value interface{}, schema map[string]interface{})

ValidateValueAgainstSchema validates a single value against its schema

Types

type Converter added in v1.2.0

type Converter struct{}

Converter provides utilities for converting values between different types.

func NewConverter added in v1.2.0

func NewConverter() *Converter

NewConverter creates a new Converter.

func (*Converter) Convert added in v1.2.0

func (c *Converter) Convert(value interface{}, targetType reflect.Type) (interface{}, error)

Convert converts a value to a specified type using various conversion strategies.

type Generator added in v1.2.0

type Generator struct {
	// Configuration options
	IncludeFieldsWithoutTags bool
}

Generator generates JSON Schema from Go types.

func NewGenerator added in v1.2.0

func NewGenerator() *Generator

NewGenerator creates a new schema generator with default configuration.

func (*Generator) GenerateSchema added in v1.2.0

func (g *Generator) GenerateSchema(v interface{}) (map[string]interface{}, error)

GenerateSchema generates a JSON Schema from a Go struct or any value.

func (*Generator) WithIncludeFieldsWithoutTags added in v1.2.0

func (g *Generator) WithIncludeFieldsWithoutTags(include bool) *Generator

WithIncludeFieldsWithoutTags configures whether to include fields without JSON tags.

type PropertyDetail added in v1.2.0

type PropertyDetail struct {
	Type        string        `json:"type"`
	Description string        `json:"description,omitempty"`
	Enum        []interface{} `json:"enum,omitempty"`
	Format      string        `json:"format,omitempty"`
	Minimum     *float64      `json:"minimum,omitempty"`
	Maximum     *float64      `json:"maximum,omitempty"`
	MinLength   *int          `json:"minLength,omitempty"`
	MaxLength   *int          `json:"maxLength,omitempty"`
	Pattern     string        `json:"pattern,omitempty"`
	Default     interface{}   `json:"default,omitempty"`
}

PropertyDetail represents a JSON Schema property definition.

type ToolInputSchema added in v1.2.0

type ToolInputSchema struct {
	Type       string                    `json:"type"`
	Properties map[string]PropertyDetail `json:"properties"`
	Required   []string                  `json:"required"`
}

ToolInputSchema represents a JSON Schema for tool input.

func FromStruct

func FromStruct(v interface{}) ToolInputSchema

FromStruct generates a ToolInputSchema from struct tags. It examines the struct fields and their tags to create a schema that describes the expected input format for an MCP tool.

type Validator added in v1.2.0

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

Validator provides validation for struct fields.

func NewValidator added in v1.2.0

func NewValidator() *Validator

NewValidator creates a new validator.

func (*Validator) Array added in v1.2.0

func (v *Validator) Array(fieldName string, array []interface{}, arraySchema map[string]interface{}) *Validator

ArrayValidator validates array elements against a schema

func (*Validator) Enum added in v1.2.0

func (v *Validator) Enum(fieldName string, value string, allowedValues []string) *Validator

Enum validates a value is in a set of allowed values.

func (*Validator) Error added in v1.2.0

func (v *Validator) Error() error

Error returns validation errors or nil if none.

func (*Validator) Errors added in v1.2.0

func (v *Validator) Errors() []string

Errors returns all validation error messages.

func (*Validator) Format added in v1.2.0

func (v *Validator) Format(fieldName string, value string, format string) *Validator

Format validates a string matches a format.

func (*Validator) HasErrors added in v1.2.0

func (v *Validator) HasErrors() bool

HasErrors returns true if there are validation errors.

func (*Validator) Map added in v1.2.0

func (v *Validator) Map(fieldName string, value map[string]interface{}, propsSchema map[string]interface{}) *Validator

MapValidator validates a map's values against a property schema

func (*Validator) Max added in v1.2.0

func (v *Validator) Max(fieldName string, value interface{}, max float64) *Validator

Max validates a numeric value has a maximum value.

func (*Validator) MaxLength added in v1.2.0

func (v *Validator) MaxLength(fieldName string, value string, maxLength int) *Validator

MaxLength validates a string has a maximum length.

func (*Validator) Min added in v1.2.0

func (v *Validator) Min(fieldName string, value interface{}, min float64) *Validator

Min validates a numeric value has a minimum value.

func (*Validator) MinLength added in v1.2.0

func (v *Validator) MinLength(fieldName string, value string, minLength int) *Validator

MinLength validates a string has a minimum length.

func (*Validator) Required added in v1.2.0

func (v *Validator) Required(fieldName string, value interface{}) *Validator

Required validates that a field is not nil or empty.

Jump to

Keyboard shortcuts

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