Documentation
¶
Overview ¶
Package jsonschema provides very simple functionality for representing a JSON schema as a (nested) struct. This struct can be used with the chat completion "function call" feature. For more complicated schemas, it is recommended to use a dedicated JSON schema library and/or pass in the schema in []byte format.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CollectDefs ¶ added in v1.40.5
func CollectDefs(def Definition) map[string]Definition
func Validate ¶ added in v1.29.0
func Validate(schema Definition, data any, opts ...ValidateOption) bool
func VerifySchemaAndUnmarshal ¶ added in v1.29.0
func VerifySchemaAndUnmarshal(schema Definition, content []byte, v any) error
Types ¶
type Definition ¶
type Definition struct {
// Type specifies the data type of the schema.
Type DataType `json:"type,omitempty"`
// Description is the description of the schema.
Description string `json:"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 []string `json:"enum,omitempty"`
// Properties describes the properties of an object, if the schema type is Object.
Properties map[string]Definition `json:"properties,omitempty"`
// Required specifies which properties are required, if the schema type is Object.
Required []string `json:"required,omitempty"`
// Items specifies which data type an array contains, if the schema type is Array.
Items *Definition `json:"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"`
// Whether the schema is nullable or not.
Nullable bool `json:"nullable,omitempty"`
// Ref Reference to a definition in $defs or external schema.
Ref string `json:"$ref,omitempty"`
// Defs A map of reusable schema definitions.
Defs map[string]Definition `json:"$defs,omitempty"`
}
Definition is a struct for describing a JSON Schema. It is fairly limited, and you may have better luck using a third-party library.
func GenerateSchemaForType ¶ added in v1.29.0
func GenerateSchemaForType(v any) (*Definition, error)
func (*Definition) MarshalJSON ¶ added in v1.14.0
func (d *Definition) MarshalJSON() ([]byte, error)
type ValidateOption ¶ added in v1.40.5
type ValidateOption func(*validateArgs)
func WithDefs ¶ added in v1.40.5
func WithDefs(defs map[string]Definition) ValidateOption
Click to show internal directories.
Click to hide internal directories.