Documentation
¶
Overview ¶
Package transforms extends the pipeline with schema validation, data transformation, and enrichment middlewares.
Index ¶
- type Schema
- type SchemaField
- type SchemaValidationMiddleware
- type Transform
- func AddTimestamp(field string) Transform
- func ComputeField(name string, compute func(fields map[string]any) any) Transform
- func DropFields(fields ...string) Transform
- func RegexReplace(field, pattern, replacement string) Transform
- func RenameField(from, to string) Transform
- func SetDefault(field string, defaultVal any) Transform
- func TrimStrings() Transform
- type TransformMiddleware
- type ValidationResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Schema ¶
type Schema struct {
Name string `json:"name" yaml:"name"`
Fields []SchemaField `json:"fields" yaml:"fields"`
Strict bool `json:"strict" yaml:"strict"` // Reject unknown fields.
}
Schema defines the expected shape and validation rules for items.
type SchemaField ¶
type SchemaField struct {
Name string `json:"name" yaml:"name"`
Type string `json:"type" yaml:"type"` // string, number, boolean, array, url, email, date
Required bool `json:"required" yaml:"required"`
Pattern string `json:"pattern" yaml:"pattern"` // regex
MinLen int `json:"min_len" yaml:"min_len"`
MaxLen int `json:"max_len" yaml:"max_len"`
}
SchemaField defines validation rules for a single field.
type SchemaValidationMiddleware ¶
type SchemaValidationMiddleware struct {
Schema *Schema
Logger *slog.Logger
OnFail string // "drop", "log", "annotate"
}
SchemaValidationMiddleware validates items against a schema and drops invalid ones.
func (*SchemaValidationMiddleware) Name ¶
func (m *SchemaValidationMiddleware) Name() string
Name returns the middleware name.
type Transform ¶
Transform is a function that modifies an item in place.
func AddTimestamp ¶
AddTimestamp adds a timestamp field to items.
func ComputeField ¶
ComputeField creates a new field by applying a function to existing fields.
func DropFields ¶
DropFields removes specified fields from items.
func RegexReplace ¶
RegexReplace applies a regex replacement on a field.
func SetDefault ¶
SetDefault sets a default value if the field is missing or empty.
func TrimStrings ¶
func TrimStrings() Transform
TrimStrings trims whitespace from all string fields.
type TransformMiddleware ¶
TransformMiddleware applies a sequence of transforms to items.
func (*TransformMiddleware) Name ¶
func (m *TransformMiddleware) Name() string
Name returns the middleware name.
type ValidationResult ¶
type ValidationResult struct {
Valid bool `json:"valid"`
Errors []string `json:"errors,omitempty"`
}
ValidationResult contains the outcome of schema validation.