Documentation
¶
Overview ¶
Package schema provides JSON Schema generation from Go types, schema registration and lookup, validation against schemas, and default value application. It supports generating schemas compliant with JSON Schema Draft 2020-12.
Index ¶
- Variables
- func ApplyDefaults(defaults *DefaultResult, v reflect.Value)
- func ConvertType(fieldName string, t reflect.Type, v any) any
- func DefaultSchemaNamer(t reflect.Type, hint string) string
- func FromField(registry core.Registry, f reflect.StructField, hint string) *core.Schema
- func FromType(r core.Registry, t reflect.Type) *core.Schema
- func JsonTagValue(r core.Registry, fieldName string, s *core.Schema, value string) any
- func Validate(r core.Registry, s *core.Schema, path *core.PathBuffer, mode core.ValidateMode, ...)
- type ConfigProvider
- type DefaultResult
- type DefaultResultPath
- type MapRegistry
- func (r *MapRegistry) Map() map[string]*core.Schema
- func (r *MapRegistry) MarshalJSON() ([]byte, error)
- func (r *MapRegistry) MarshalYAML() (any, error)
- func (r *MapRegistry) RegisterTypeAlias(t reflect.Type, alias reflect.Type)
- func (r *MapRegistry) RegistryConfig() RegistryConfig
- func (r *MapRegistry) Schema(t reflect.Type, allowRef bool, hint string) *core.Schema
- func (r *MapRegistry) SchemaFromRef(ref string) *core.Schema
- func (r *MapRegistry) TypeFromRef(ref string) reflect.Type
- type ModelValidator
- type RegistryConfig
Constants ¶
This section is empty.
Variables ¶
var DefaultArrayNullable = true
DefaultArrayNullable controls whether generated schemas for slice types are nullable by default. Set to false to make arrays non-nullable.
var ValidateStrictCasing = false
ValidateStrictCasing controls whether property name matching during validation is case-sensitive. When false (the default), property names are matched case-insensitively.
Functions ¶
func ApplyDefaults ¶
func ApplyDefaults(defaults *DefaultResult, v reflect.Value)
ApplyDefaults sets zero-valued fields in v to their default values as described by the given DefaultResult.
func ConvertType ¶
ConvertType converts a value to the target type, handling slices, pointers, and scalar conversions. It panics if the conversion is not possible.
func DefaultSchemaNamer ¶
DefaultSchemaNamer generates a human-readable schema name from a Go type. It strips package paths, uppercases the first letter, and handles generics and slices (e.g. []int becomes ListInt).
func FromField ¶
FromField generates a schema for a struct field, applying struct tag constraints such as doc, format, default, enum, minimum, maximum, pattern, and others.
func FromType ¶
FromType generates a JSON Schema from a Go type. It handles primitives, slices, maps, structs, and types that implement core.SchemaProvider or core.SchemaTransformer.
func JsonTagValue ¶
JsonTagValue parses a struct tag value string into a Go value according to the schema's type. Strings are returned as-is, while other types are parsed as JSON.
func Validate ¶
func Validate(r core.Registry, s *core.Schema, path *core.PathBuffer, mode core.ValidateMode, v any, res *core.ValidateResult)
Validate checks a value against a schema, appending any violations to res. It resolves $ref references, enforces type constraints, format validation, and composition keywords (oneOf, anyOf, allOf, not).
Types ¶
type ConfigProvider ¶
type ConfigProvider interface {
RegistryConfig() RegistryConfig
}
ConfigProvider is implemented by registries that expose their configuration.
type DefaultResult ¶
type DefaultResult struct {
Paths []DefaultResultPath
}
DefaultResult holds a collection of default values discovered for a type, each associated with the struct field index path where the default applies.
func FindDefaults ¶
func FindDefaults(registry core.Registry, t reflect.Type) *DefaultResult
FindDefaults walks the given type and returns all fields that have a "default" struct tag, along with their parsed default values.
func (*DefaultResult) Every ¶
Every calls f for each default value path, traversing into the given value to locate the target field. Slices and maps are traversed recursively.
func (*DefaultResult) EveryPB ¶
func (r *DefaultResult) EveryPB(pb *core.PathBuffer, v reflect.Value, f func(reflect.Value, any))
EveryPB is like Every but also tracks the JSON path using a PathBuffer, which is useful for reporting the location of applied defaults.
type DefaultResultPath ¶
DefaultResultPath represents a single default value located at a specific field path within a struct hierarchy.
type MapRegistry ¶
type MapRegistry struct {
// contains filtered or unexported fields
}
MapRegistry is the default core.Registry implementation that stores schemas in a map keyed by type name. It handles schema generation, caching, and reference resolution for OpenAPI components.
func NewMapRegistry ¶
func NewMapRegistry(namer func(t reflect.Type, hint string) string) *MapRegistry
NewMapRegistry creates a new MapRegistry with the given naming function. Use DefaultSchemaNamer for the standard naming convention.
func NewMapRegistryWithConfig ¶
func NewMapRegistryWithConfig(namer func(t reflect.Type, hint string) string, config RegistryConfig) *MapRegistry
NewMapRegistryWithConfig creates a new MapRegistry with the given naming function and configuration overrides.
func (*MapRegistry) Map ¶
func (r *MapRegistry) Map() map[string]*core.Schema
Map returns the underlying map of schema names to schema definitions.
func (*MapRegistry) MarshalJSON ¶
func (r *MapRegistry) MarshalJSON() ([]byte, error)
MarshalJSON serializes the registry's schemas as JSON.
func (*MapRegistry) MarshalYAML ¶
func (r *MapRegistry) MarshalYAML() (any, error)
MarshalYAML serializes the registry's schemas as YAML.
func (*MapRegistry) RegisterTypeAlias ¶
func (r *MapRegistry) RegisterTypeAlias(t reflect.Type, alias reflect.Type)
RegisterTypeAlias registers an alias so that schemas requested for type t are generated using the alias type instead.
func (*MapRegistry) RegistryConfig ¶
func (r *MapRegistry) RegistryConfig() RegistryConfig
RegistryConfig returns the configuration for this registry.
func (*MapRegistry) Schema ¶
Schema returns the schema for the given type, generating and caching it if needed. When allowRef is true and the type is a named struct, a $ref schema is returned instead of the full definition.
func (*MapRegistry) SchemaFromRef ¶
func (r *MapRegistry) SchemaFromRef(ref string) *core.Schema
SchemaFromRef resolves a $ref string to its corresponding schema, or returns nil if the ref does not match a known schema.
func (*MapRegistry) TypeFromRef ¶
func (r *MapRegistry) TypeFromRef(ref string) reflect.Type
TypeFromRef returns the Go type associated with the given $ref string.
type ModelValidator ¶
type ModelValidator struct {
// contains filtered or unexported fields
}
ModelValidator validates Go values against their auto-generated JSON Schemas. It maintains a reusable registry, path buffer, and result to reduce allocations across repeated validations.
func NewModelValidator ¶
func NewModelValidator() *ModelValidator
NewModelValidator returns a new ModelValidator with a default registry.
type RegistryConfig ¶
RegistryConfig controls schema generation behavior for a registry.