schema

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package schema provides a runtime registry for typed record schemas and validates incoming maps against the declared field definitions.

Index

Constants

This section is empty.

Variables

View Source
var Default = NewRegistry()

Default is the package-level Registry shared by callers that do not need a private one.

Functions

This section is empty.

Types

type DataType

type DataType int

DataType enumerates the field types understood by the schema validator.

const (
	TypeInt DataType = iota
	TypeInt64
	TypeFloat
	TypeBool
	TypeString
	TypeTime
	TypeArray
	TypeMap
	TypeAny
)

Field type identifiers used by Schema definitions.

func InferType

func InferType(v any) DataType

InferType maps a runtime value to its DataType.

nil and any unrecognised value return TypeAny. float32 and float64 both map to TypeFloat. Only []any and map[string]any are recognised as containers; other slice or map types fall back to TypeAny.

func (DataType) String

func (t DataType) String() string

String returns the textual name of the DataType.

type FieldDef

type FieldDef struct {
	// Name is the field key matched against the input map.
	Name string
	// Type is the expected DataType of the field value.
	Type DataType
	// Required reports whether an absent key is an error.
	Required bool
	// Default, when non-nil, fills an absent key with this value (and so also
	// suppresses the required-missing error). Callers must use a typed value
	// (for example float64(0)) so that the interface is non-nil even when the
	// default is a zero value.
	Default any
}

FieldDef describes a single field of a Schema.

type MultiError

type MultiError struct {
	// Errors holds the individual validation errors in the order they were found.
	Errors []error
}

MultiError aggregates one or more validation errors into a single value. It is returned by Validate when more than one problem is found.

func (*MultiError) Append

func (m *MultiError) Append(err error)

Append records err when it is non-nil.

func (*MultiError) Err

func (m *MultiError) Err() error

Err returns nil when no errors were collected, or the receiver itself.

func (*MultiError) Error

func (m *MultiError) Error() string

Error joins the collected error messages with a semicolon separator.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry stores named Schemas and is safe for concurrent use.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty Registry ready to accept registrations.

func (*Registry) Get

func (r *Registry) Get(name string) (Schema, bool)

Get returns the Schema registered under name and a found flag.

func (*Registry) MustGet

func (r *Registry) MustGet(name string) Schema

MustGet returns the Schema registered under name and panics when missing.

func (*Registry) Register

func (r *Registry) Register(s Schema) error

Register stores s under s.Name. It returns an error when the name is empty or already registered.

type Schema

type Schema struct {
	// Name uniquely identifies the schema within a Registry.
	Name string
	// Fields is the ordered list of field definitions.
	Fields []FieldDef
	// Strict, when true, makes unknown keys in the input map an error.
	Strict bool
}

Schema is a named, ordered set of field definitions.

func (*Schema) Validate

func (s *Schema) Validate(data map[string]any) error

Validate reports every problem found in data with respect to s.

An absent key errors only when the field is Required and has no Default; a field with a non-nil Default is filled with that value in data. Present values are checked with InferType; numeric fields (int, int64, float) accept any numeric value interchangeably, and TypeAny accepts every value including nil. When Strict is true, keys in data that are not declared as fields are reported as errors. All problems are aggregated and returned together, with a nil result when data is clean.

Jump to

Keyboard shortcuts

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