Documentation
¶
Overview ¶
Package types provides the core validation engine types and rule system.
The types package contains the fundamental building blocks for the validation system including canonical rule representations, compilation of rules into executable validator functions, parsing of struct tags into rules, and rule type identifiers. It provides the unified AST (Abstract Syntax Tree) for validation rules, ensuring consistency between tag-based and builder-based validation. The package includes comprehensive regex safety measures and Unicode-aware string validation, and is designed to be extensible, allowing custom rule types to be registered and compiled through the plugin system.
Package types contains compiler helpers and rule primitives.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterRule ¶
func RegisterRule(kind Kind, rc RuleCompiler)
RegisterRule registers a global custom Rule compiler. Call this at init.
Types ¶
type Compiler ¶
type Compiler struct {
// contains filtered or unexported fields
}
Compiler compiles rules into validator functions.
func NewCompiler ¶
func NewCompiler(t translator.Translator) *Compiler
NewCompiler creates a new compiler with the given translator.
func (*Compiler) Compile ¶
func (c *Compiler) Compile(rules []Rule) ValidatorFunc
Compile compiles a slice of rules into a validator function.
func (*Compiler) CompileField ¶
func (c *Compiler) CompileField(rules []Rule) FieldValidator
CompileField compiles rules for struct field validation.
func (*Compiler) RegisterRule ¶
func (c *Compiler) RegisterRule(kind Kind, rc RuleCompiler)
RegisterRule registers a custom rule compiler for this compiler instance.
type FieldValidator ¶
FieldValidator represents a field-specific validation function.
type Kind ¶
type Kind string
Kind represents the type of validation rule.
This type is used to identify different validation rule types in the AST.
const ( // String validation kinds KString Kind = "string" KLength Kind = "length" KMinLength Kind = "minLength" KMaxLength Kind = "maxLength" KRegex Kind = "regex" KOneOf Kind = "oneOf" KMinRunes Kind = "minRunes" KMaxRunes Kind = "maxRunes" // Integer validation kinds KInt Kind = "int" KInt64 Kind = "int64" KMinInt Kind = "minInt" KMaxInt Kind = "maxInt" // Slice validation kinds KSlice Kind = "slice" KSliceLength Kind = "sliceLength" KMinSliceLength Kind = "minSliceLength" KMaxSliceLength Kind = "maxSliceLength" KForEach Kind = "forEach" // Boolean validation kinds KBool Kind = "bool" )
type Rule ¶
type Rule struct {
Kind Kind
Args map[string]any // e.g. {"n": int64(3), "pattern": ".*"}
Elem *Rule // For nested rules (e.g., slice element validation)
}
Rule represents a single validation rule with its arguments.
Fields:
- Kind: The type of validation rule.
- Args: Map of rule-specific arguments (e.g., {"n": int64(3), "pattern": ".*"}).
- Elem: For nested rules (e.g., slice element validation).
func NewRuleWithElem ¶
NewRuleWithElem builds a Rule with an element sub-rule for nesting.
type RuleCompiler ¶
RuleCompiler compiles a single Rule into a validate function. Implementations may precompute heavy state (e.g., compiled regex).
type ValidatorFunc ¶
ValidatorFunc represents a compiled validation function.