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"
)

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.

InterfaceOption configures a registered interface field.

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

func Discriminator(name string) InterfaceOption

Discriminator sets the JSON property used to distinguish interface cases.

func Impl[T any](value string, impl T) InterfaceOption

Impl registers an interface implementation with its stable wire value.

type InterfaceOptionObj struct{}

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.

Nullable represents a required object property whose value may be null. The zero value encodes as null; Present reports whether Value is non-null.

type Nullable[T any] struct {
Present bool
Value T
}

func (Nullable[T]) IsZero() bool

IsZero always reports false so json:“,omitzero” cannot omit a required nullable property.

func (n Nullable[T]) MarshalJSON() ([]byte, error)

MarshalJSON encodes null or a present non-null value.

func (n *Nullable[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes null or a present value without mutating the receiver when decoding fails.

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)

Optional represents an object property that may be absent. The zero value is absent; a present value may contain T’s zero value but may not encode as null. Containing struct fields must use json:“,omitzero” so absent values are omitted before MarshalJSON is called.

type Optional[T any] struct {
Present bool
Value T
}

func (o Optional[T]) IsZero() bool

IsZero reports whether the property is absent.

func (o Optional[T]) MarshalJSON() ([]byte, error)

MarshalJSON encodes a present non-null value.

func (o *Optional[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes a present non-null value without mutating the receiver when decoding fails.

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 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 AsRef() SchemaMethodOption

AsRef requests that, wherever this type is referenced from another registered schema, it be rendered as a “$ref” into that schema’s “$defs” instead of being inlined.

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

func WithEnum[T any](field T) SchemaMethodOption

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

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

func WithInterface[T any](field T, options ...InterfaceOption) 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 WithStringerEnum[T any](field T) SchemaMethodOption

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
}

Generated by gomarkdoc