schema

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 6, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package schema provides JSON Schema utilities for structured output.

Index

Constants

View Source
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

This section is empty.

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 Array

func Array(items *Schema) *Schema

Array creates an array schema.

func Boolean

func Boolean() *Schema

Boolean creates a boolean schema.

func Describe

func Describe(s *Schema, description string) *Schema

Describe adds a description to a schema.

func FromType

func FromType(v any) (*Schema, error)

FromType generates a JSON Schema from a Go type. Supports basic types, structs, slices, and maps. Adds $schema URI to root object schemas.

func Integer

func Integer() *Schema

Integer creates an integer schema.

func MustFromType

func MustFromType(v any) *Schema

MustFromType generates a JSON Schema from a Go type, panicking on error.

func Null

func Null() *Schema

Null creates a null schema.

func Nullable

func Nullable(s *Schema) *Schema

Nullable makes a schema nullable by wrapping it in anyOf with null.

func Number

func Number() *Schema

Number creates a number schema.

func Object

func Object(properties map[string]*Schema) *Schema

Object creates an object schema.

func String

func String() *Schema

String creates a string schema.

func StringEnum

func StringEnum(values ...string) *Schema

StringEnum creates a string schema with allowed values.

func ToStrict

func ToStrict(s *Schema) *Schema

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

func (s *Schema) JSON() ([]byte, error)

JSON returns the schema as a JSON-encoded byte slice.

func (*Schema) MarshalJSON

func (s *Schema) MarshalJSON() ([]byte, error)

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.

func (*Schema) MustJSON

func (s *Schema) MustJSON() []byte

MustJSON returns the schema as JSON or panics.

func (*Schema) String

func (s *Schema) String() string

String returns the schema as a JSON string.

Jump to

Keyboard shortcuts

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