Documentation
¶
Overview ¶
Package schema provides utilities for generating JSON Schema from Go structs.
Index ¶
- func HandleArgs[T any](arguments any) (*T, error)
- func HandleArgsWithSchema[T any](arguments any, schemaMap map[string]interface{}) (*T, error)
- func ValidateAndConvertArgs(schemaMap map[string]interface{}, args map[string]interface{}, ...) (interface{}, error)
- func ValidateConstraints(v *Validator, fieldName string, value interface{}, ...)
- func ValidateStruct(data interface{}) error
- func ValidateType(value interface{}, expectedType string) bool
- func ValidateValueAgainstSchema(v *Validator, fieldName string, value interface{}, ...)
- type Converter
- type Generator
- type PropertyDetail
- type ToolInputSchema
- type Validator
- func (v *Validator) Array(fieldName string, array []interface{}, arraySchema map[string]interface{}) *Validator
- func (v *Validator) Enum(fieldName string, value string, allowedValues []string) *Validator
- func (v *Validator) Error() error
- func (v *Validator) Errors() []string
- func (v *Validator) Format(fieldName string, value string, format string) *Validator
- func (v *Validator) HasErrors() bool
- func (v *Validator) Map(fieldName string, value map[string]interface{}, ...) *Validator
- func (v *Validator) Max(fieldName string, value interface{}, max float64) *Validator
- func (v *Validator) MaxLength(fieldName string, value string, maxLength int) *Validator
- func (v *Validator) Min(fieldName string, value interface{}, min float64) *Validator
- func (v *Validator) MinLength(fieldName string, value string, minLength int) *Validator
- func (v *Validator) Required(fieldName string, value interface{}) *Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HandleArgs ¶
HandleArgs is a helper function that handles parsing and validating tool arguments into a strongly-typed struct.
func HandleArgsWithSchema ¶ added in v1.2.0
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
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.
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
GenerateSchema generates a JSON Schema from a Go struct or any value.
func (*Generator) WithIncludeFieldsWithoutTags ¶ added in v1.2.0
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,omitempty"`
}
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) HasErrors ¶ added in v1.2.0
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