Documentation
¶
Overview ¶
Package schema provides a type-safe validation system for structured data.
It defines a simple type system with built-in types (string, int, float, bool) and support for slices and custom validators. Schemas map field names to types, enabling runtime validation of complex data structures.
Basic usage:
schema := schema.Schema{
"api_key": schema.String(),
"retries": schema.Int(),
"tags": schema.Slice(schema.String()),
}
data := map[string]any{
"api_key": "secret123",
"retries": 3,
"tags": []string{"prod", "critical"},
}
if err := schema.Validate(schema, data); err != nil {
// Handle validation errors
}
Schemas can be created programmatically or parsed from type strings:
typeMap := map[string]string{
"api_key": "string",
"retries": "int",
"tags": "[string]",
}
schema, err := schema.ParseTypeMap(typeMap)
Custom validators can be registered for domain-specific validation:
positiveInt := schema.Custom("positive_int", func(v any) error {
i, ok := v.(int)
if !ok {
return fmt.Errorf("expected int")
}
if i <= 0 {
return fmt.Errorf("must be positive")
}
return nil
})
This package is designed to be library-agnostic, with zero external dependencies beyond the Go standard library. It can be embedded in larger systems or extracted as a standalone library.
Index ¶
- func Validate(schema Schema, data map[string]any) error
- func ValidateFields(schema Schema, data map[string]any, fields ...string) error
- func ValidationErrors(err error) []error
- type AggregateError
- type BoolType
- type CustomType
- type FloatType
- type IntType
- type Schema
- type SliceType
- type StringType
- type Type
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Validate ¶
Validate checks if data conforms to the schema. Returns an error with all validation failures found.
func ValidateFields ¶
ValidateFields validates only specific fields from data against the schema. Missing fields are treated as an error.
func ValidationErrors ¶
ValidationErrors returns all validation errors if err is an AggregateError. Otherwise returns nil.
Types ¶
type AggregateError ¶
type AggregateError struct {
Errors []error
}
AggregateError represents multiple validation failures.
func (*AggregateError) Error ¶
func (e *AggregateError) Error() string
type CustomType ¶
type CustomType struct {
// contains filtered or unexported fields
}
CustomType applies a user-defined validation function.
func (*CustomType) Name ¶
func (t *CustomType) Name() string
func (*CustomType) Validate ¶
func (t *CustomType) Validate(value any) error
type Schema ¶
Schema is a map of field names to their expected types. Example: {"api_key": String(), "retries": Int(), "tags": Slice(String())}
func ParseTypeMap ¶
ParseTypeMap converts a map of field names to type strings into a Schema. Example: {"api_key": "string", "retries": "int"}
func (Schema) MarshalJSON ¶
MarshalJSON serializes the schema as a map of field names to type strings.
func (*Schema) UnmarshalJSON ¶
UnmarshalJSON deserializes the schema from a map of field names to type strings.
type SliceType ¶
type SliceType struct {
// contains filtered or unexported fields
}
SliceType validates slices of a specific element type.
type StringType ¶
type StringType struct{}
StringType validates string values.
func (*StringType) Name ¶
func (t *StringType) Name() string
func (*StringType) Validate ¶
func (t *StringType) Validate(value any) error
type Type ¶
type Type interface {
// Name returns the human-readable name of the type (e.g., "string", "int").
Name() string
// Validate checks if a value conforms to this type.
Validate(value any) error
}
Type defines the contract for field validation. Implementations determine how values are validated against a type.
func ParseType ¶
ParseType converts a string type name to a Type. Supports basic types: "string", "int", "float", "bool", "[string]", "[int]", etc.
type ValidationError ¶
type ValidationError struct {
Key string // Field name
Reason string // Human-readable reason for failure
Value any // The value that failed validation
}
ValidationError represents a single field validation failure.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string