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 ¶
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.
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.
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 ¶
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.