Documentation
¶
Overview ¶
Package schema provides JSON Schema utilities for structured output.
Index ¶
- Constants
- func RegisterTypeOverride(t reflect.Type, fn func(*Schema))
- type Schema
- func Array(items *Schema) *Schema
- func Boolean() *Schema
- func Describe(s *Schema, description string) *Schema
- func FromType(v any) (*Schema, error)
- func Integer() *Schema
- func MustFromType(v any) *Schema
- func Null() *Schema
- func Nullable(s *Schema) *Schema
- func Number() *Schema
- func Object(properties map[string]*Schema) *Schema
- func String() *Schema
- func StringEnum(values ...string) *Schema
- func ToStrict(s *Schema) *Schema
Constants ¶
const DefaultSchemaURI = "https://json-schema.org/draft/2020-12/schema"
DefaultSchemaURI is the default JSON Schema URI used for generated schemas.
Variables ¶
This section is empty.
Functions ¶
func RegisterTypeOverride ¶ added in v0.1.4
RegisterTypeOverride installs a post-processing hook for a specific reflect.Type. After fromReflectType produces its default schema for values of that type, fn is called to mutate the result (typically to set Format).
Types ¶
type Schema ¶
type Schema struct {
// SchemaURI is the $schema URI (e.g., "https://json-schema.org/draft/2020-12/schema").
SchemaURI string `json:"$schema,omitempty"`
// Type is the JSON type (object, array, string, number, integer, boolean, null).
Type string `json:"type,omitempty"`
// Title is a short description of the schema.
Title string `json:"title,omitempty"`
// Description is a detailed description of the schema.
Description string `json:"description,omitempty"`
// Properties are the object properties (for object types).
Properties map[string]*Schema `json:"properties,omitempty"`
// Required lists required property names.
Required []string `json:"required,omitempty"`
// AdditionalProperties controls additional properties (for object types).
AdditionalProperties *bool `json:"additionalProperties,omitempty"`
// Items is the schema for array items (for array types).
Items *Schema `json:"items,omitempty"`
// Enum lists allowed values.
Enum []any `json:"enum,omitempty"`
// Const is the only allowed value.
Const any `json:"const,omitempty"`
// Default is the default value.
Default any `json:"default,omitempty"`
// MinLength is the minimum string length.
MinLength *int `json:"minLength,omitempty"`
// MaxLength is the maximum string length.
MaxLength *int `json:"maxLength,omitempty"`
// Pattern is a regex pattern for strings.
Pattern string `json:"pattern,omitempty"`
// Format is a semantic format (e.g., "email", "date-time").
Format string `json:"format,omitempty"`
// Minimum is the minimum numeric value.
Minimum *float64 `json:"minimum,omitempty"`
// Maximum is the maximum numeric value.
Maximum *float64 `json:"maximum,omitempty"`
// ExclusiveMinimum is the exclusive minimum numeric value.
ExclusiveMinimum *float64 `json:"exclusiveMinimum,omitempty"`
// ExclusiveMaximum is the exclusive maximum numeric value.
ExclusiveMaximum *float64 `json:"exclusiveMaximum,omitempty"`
// MultipleOf specifies that the value must be a multiple of this number.
MultipleOf *float64 `json:"multipleOf,omitempty"`
// MinItems is the minimum array length.
MinItems *int `json:"minItems,omitempty"`
// MaxItems is the maximum array length.
MaxItems *int `json:"maxItems,omitempty"`
// UniqueItems requires array items to be unique.
UniqueItems *bool `json:"uniqueItems,omitempty"`
// AnyOf allows the value to match any of the sub-schemas.
AnyOf []*Schema `json:"anyOf,omitempty"`
// OneOf requires the value to match exactly one sub-schema.
OneOf []*Schema `json:"oneOf,omitempty"`
// AllOf requires the value to match all sub-schemas.
AllOf []*Schema `json:"allOf,omitempty"`
// Not requires the value to not match the sub-schema.
Not *Schema `json:"not,omitempty"`
// Ref is a reference to another schema.
Ref string `json:"$ref,omitempty"`
// CustomRef is a non-standard reference used by some tools (e.g., opencode).
// This is "ref" (not "$ref") and typically contains a type name.
CustomRef string `json:"-"` // Handled in MarshalJSON
// Definitions contains reusable schema definitions.
Definitions map[string]*Schema `json:"$defs,omitempty"`
}
Schema represents a JSON Schema.
func FromType ¶
FromType generates a JSON Schema from a Go type. Supports basic types, structs, slices, and maps. Adds $schema URI to root object schemas.
func MustFromType ¶
MustFromType generates a JSON Schema from a Go type, panicking on error.
func StringEnum ¶
StringEnum creates a string schema with allowed values.
func ToStrict ¶
ToStrict modifies a schema to be compatible with strict mode: 1. Adds "additionalProperties": false to all object types 2. Ensures all properties are listed in "required"
func (*Schema) MarshalJSON ¶
MarshalJSON implements custom JSON marshaling to ensure object schemas always have a "properties" field (even if empty), as required by OpenAI. Uses ordered map to ensure $schema comes first.