jsonschema

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package jsonschema provides JSON Schema conversion for GoZod schemas.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedJSONSchemaType    = errors.New("unsupported JSON Schema type")
	ErrUnsupportedJSONSchemaKeyword = errors.New("unsupported JSON Schema keyword")
	ErrInvalidJSONSchema            = errors.New("invalid JSON Schema")
	ErrJSONSchemaCircularRef        = errors.New("circular reference detected in JSON Schema")
	ErrJSONSchemaPatternCompile     = errors.New("failed to compile JSON Schema pattern")
	ErrJSONSchemaIfThenElse         = errors.New("if/then/else is not supported")
	ErrJSONSchemaPatternProperties  = errors.New("patternProperties is not supported")
	ErrJSONSchemaDynamicRef         = errors.New("$dynamicRef is not supported")
	ErrJSONSchemaUnevaluatedProps   = errors.New("unevaluatedProperties is not supported")
	ErrJSONSchemaUnevaluatedItems   = errors.New("unevaluatedItems is not supported")
	ErrJSONSchemaDependentSchemas   = errors.New("dependentSchemas is not supported")
	ErrJSONSchemaPropertyNames      = errors.New("propertyNames is not supported")
	ErrJSONSchemaContains           = errors.New("contains/minContains/maxContains is not supported")
)

Conversion errors for FromJSONSchema operations.

View Source
var (
	ErrUnsupportedInputType          = errors.New("unsupported input type")
	ErrCircularReference             = errors.New("circular reference detected")
	ErrUnrepresentableType           = errors.New("unrepresentable type")
	ErrSchemaNotObjectOrStruct       = errors.New("schema is not a ZodObject or ZodStruct")
	ErrSliceElementNotSchema         = errors.New("slice element is not a ZodSchema")
	ErrArrayItemNotSchema            = errors.New("array item is not a ZodSchema")
	ErrUnhandledArrayLike            = errors.New("unhandled array-like type")
	ErrUnionInvalid                  = errors.New("schema is not a union type with Options method")
	ErrUnionNoMembers                = errors.New("union has no member schemas")
	ErrIntersectionInvalid           = errors.New("schema is not an intersection type")
	ErrInvalidEnumSchema             = errors.New("invalid enum schema")
	ErrEnumExtractValues             = errors.New("unable to extract enum values")
	ErrLiteralNoValuesMethod         = errors.New("schema does not have a Values method")
	ErrLiteralUnexpectedReturnValues = errors.New("unexpected number of return values from Values method")
	ErrExpectedDiscriminatedUnion    = errors.New("expected a discriminated union schema")
	ErrExpectedRecord                = errors.New("expected a record schema with ValueType()")
	ErrRecordValueNotSchema          = errors.New("record value type is not a valid schema")
	ErrMapNoMethods                  = errors.New("schema does not implement KeyType() and ValueType() methods for map conversion")
	ErrMapKeyNotSchema               = errors.New("map key type is not a valid schema")
	ErrMapValueNotSchema             = errors.New("map value type is not a valid schema")
)

Conversion errors for ToJSONSchema operations.

Functions

func FromJSONSchema

func FromJSONSchema(schema *lib.Schema, opts ...FromJSONSchemaOptions) (core.ZodSchema, error)

FromJSONSchema converts a kaptinlin/jsonschema Schema to a GoZod schema. Returns core.ZodSchema for maximum flexibility.

func ToJSONSchema

func ToJSONSchema(input any, opts ...Options) (*lib.Schema, error)

ToJSONSchema converts a GoZod schema or registry into a JSON Schema instance.

Types

type FromJSONSchemaOptions

type FromJSONSchemaOptions struct {
	// StrictMode causes conversion to fail on unsupported features.
	// If false, unsupported features are silently ignored.
	StrictMode bool
}

FromJSONSchemaOptions configures the JSON Schema to GoZod conversion.

type Options

type Options struct {
	// A registry used to look up metadata for each schema.
	// Any schema with an ID property will be extracted as a $def.
	Metadata *core.Registry[core.GlobalMeta]

	// How to handle unrepresentable types:
	// "throw" (default) - Unrepresentable types throw an error.
	// "any" - Unrepresentable types become {}.
	Unrepresentable string

	// How to handle cycles:
	// "ref" (default) - Cycles will be broken using $defs.
	// "throw" - Cycles will throw an error if encountered.
	Cycles string

	// How to handle reused schemas:
	// "inline" (default) - Reused schemas will be inlined.
	// "ref" - Reused schemas will be extracted as $defs.
	Reused string

	// A function used to convert ID values to URIs for external $refs.
	URI func(id string) string

	// Target specifies the JSON Schema version.
	// "draft-2020-12" (default) or "draft-07".
	Target string

	// Override is a custom logic to modify the schema after generation.
	Override func(ctx OverrideContext)

	// IO specifies whether to convert the "input" or "output" schema.
	// "output" (default) or "input".
	IO string
}

Options defines the configuration options for JSON schema conversion.

type OverrideContext

type OverrideContext struct {
	ZodSchema  core.ZodSchema
	JSONSchema *lib.Schema
}

OverrideContext provides context for the Override function.

Jump to

Keyboard shortcuts

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