Skip to content

API Reference

import "github.com/tylergannon/go-gen-jsonschema"

type DataType string

const (
Object DataType = "object"
Number DataType = "number"
Integer DataType = "integer"
String DataType = "string"
Array DataType = "array"
Null DataType = "null"
Boolean DataType = "boolean"
)

Enum options (v1) - stubs for scanning/type-checking; parsed by scanner

type EnumMode int

const (
EnumStrings EnumMode = iota + 1
)

type EnumType struct{}

func NewEnumType[T ~string]() EnumType

NewEnumType denotes that the type argument should be an enum. If called in the same package where the type is declared, then it applies globally. In all cases, the const values MUST be declared in the same package as the call to NewEnumType.

For now, only string types are supported.

type InterfaceMarker struct{}

func NewInterfaceImpl[T any](...T) InterfaceMarker

NewInterfaceImpl marks the arguments as possible implementations for the interface type given in the type argument.

  1. If called in the same package as the interface itself, then all global instances can be replaced.
  2. If called somewhere else, only applies to the local package.

JSONSchema is a struct for describing a JSON Schema. It is fairly limited, and you may have better luck using a third-party library. This is a copy from go-openai’s “jsonschema.Definition{}” struct, with the difference being that this one holds references to json.Marshaler, rather than to itself.

type JSONSchema struct {
// Type specifies the data type of the schema.
Type DataType `json:"type" yaml:"type"`
// Description is the description of the schema.
Description string `json:"description,omitempty" yaml:"description,omitempty"`
// Enum is used to restrict a value to a fixed set of values. It must be an
// array with at least one element, where each element is unique. You will
// probably only use this with strings.
Enum []any `json:"enum,omitempty" yaml:"enum,omitempty"`
// Properties describes the properties of an object, if the schema type is
// Object.
Properties map[string]SchemaNode `json:"properties,omitempty" yaml:"properties,omitempty"`
// Required specifies which properties are required, if the schema type is
// Object.
Required []string `json:"required,omitempty" yaml:"required,omitempty"`
// Items specifies which data type an array contains, if the schema type is
// Array.
Items SchemaNode `json:"items,omitempty" yaml:"items,omitempty"`
// AdditionalProperties is used to control the handling of properties in an
// object that are not explicitly defined in the properties section of the
// schema. example: additionalProperties: true additionalProperties: false
// additionalProperties: jsonschema.Definition{Type: jsonschema.String}
AdditionalProperties any `json:"additionalProperties,omitempty" yaml:"additionalProperties,omitempty"`
Definitions map[string]SchemaNode `json:"$defs,omitzero" yaml:"$defs,omitempty"`
Const any `json:"const,omitempty"` // Provide a const value
// Strict will make all properties required and additionalProperties: false if
// not already set. pplies only if Type = "object".
Strict bool `json:"-" yaml:"-"`
}

func (s JSONSchema) MarshalJSON() ([]byte, error)

type JSONUnionType []*JSONSchema

func (j JSONUnionType) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

type ObjectSchema struct {
Properties []SchemaProperty
Strict bool
Required []string
Description string
AdditionalProperties any
}

func (s *ObjectSchema) AddProperty(key string, value SchemaNode)

func (s *ObjectSchema) AddRequiredProperty(key string, value SchemaNode)

func (s ObjectSchema) MarshalJSON() ([]byte, error)

type ParentSchema struct {
*ObjectSchema
Definitions []SchemaProperty
// The key name for the definitions map. Defaults to "definitions"
DefinitionsKeyName string `json:"-"`
Title string `json:"title,omitzero"`
}

func (s *ParentSchema) AddDefinition(key string, value SchemaNode)

func (s ParentSchema) MarshalJSON() ([]byte, error)

type SchemaFunction func() json.RawMessage

type SchemaMarker struct{}

func NewJSONSchemaBuilder[T any](SchemaFunction) SchemaMarker

NewJSONSchemaBuilder registers a function as being a stub that should be implemented with a proper json schema and, as needed, unmarshaler functionality.

func NewJSONSchemaBuilderFor(_ any, _ SchemaFunction, _ ...SchemaMethodOption) SchemaMarker

NewJSONSchemaBuilderFor registers a zero-arg builder function for the given example instance value (e.g., TypeName{}), allowing type inference without generics.

func NewJSONSchemaFunc[T any](f SchemaMethod[T], _ ...SchemaMethodOption) SchemaMarker

NewJSONSchemaFunc registers a free function that takes the receiver as its sole parameter as a schema entrypoint. It is equivalent to NewJSONSchemaMethod.

func NewJSONSchemaMethod[T any](SchemaMethod[T], ...SchemaMethodOption) SchemaMarker

NewJSONSchemaMethod registers a struct method as a stub that will be implemented with a proper json schema and, as needed, unmarshaler functionality.

type SchemaMethod[T any] func(T) json.RawMessage

type SchemaMethodOption interface {
// contains filtered or unexported methods
}

func WithDiscriminator[T any](field T, name string) SchemaMethodOption

func WithEnum[T any](field T) SchemaMethodOption

func WithEnumMode(mode EnumMode) SchemaMethodOption

func WithEnumName[T any](value T, name string) SchemaMethodOption

func WithFunction[T any](val T, f func(T) json.Marshaler) SchemaMethodOption

func WithInterface[T any](field T) SchemaMethodOption

Interface options (v1) - stubs for scanning/type-checking; parsed by scanner

func WithInterfaceImpls[T any](field T, impls ...any) SchemaMethodOption

func WithRenderProviders() SchemaMethodOption

WithRenderProviders requests generation of RenderedSchema() and provider execution at runtime.

func WithStructAccessorMethod[T, U any](val T, f func(U) json.Marshaler) SchemaMethodOption

func WithStructFunctionMethod[T, U any](val U, f func(T, U) json.Marshaler) SchemaMethodOption

type SchemaMethodOptionObj struct{}

type SchemaNode = json.Marshaler

func ArraySchema(items SchemaNode, description string) SchemaNode

func BoolSchema(description string) SchemaNode

func ConstSchema[T ~int | ~string](val T, description string) SchemaNode

func EnumSchema[T ~int | ~string](description string, vals ...T) SchemaNode

func IntSchema(description string) SchemaNode

func RefSchemaEl(ref string) SchemaNode

A ref into definitions

func StringSchema(description string) SchemaNode

func UnionSchemaEl(alts ...SchemaNode) SchemaNode

An anyOf element

type SchemaProperty struct {
Key string
Value SchemaNode
}

type Tool interface {
Name() string
Description() string
Parameters() json.RawMessage
Execute(ctx context.Context, params json.RawMessage) (json.RawMessage, error)
}

func BuildTool(fn any) Tool

Generated by gomarkdoc