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 ¶
- func GetGlobalSupportedTypes() []string
- func IsGlobalTypeRegistered(name string) bool
- func RegisterGlobalType(name string, factory TypeValidatorFactory)
- func RegisterRule(kind Kind, rc RuleCompiler)
- type Compiler
- type FieldValidator
- type Kind
- type Rule
- type RuleCompiler
- type TypeRegistry
- type TypeValidator
- type TypeValidatorFactory
- type ValidatorFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetGlobalSupportedTypes ¶ added in v3.0.3
func GetGlobalSupportedTypes() []string
GetGlobalSupportedTypes returns all globally registered types.
func IsGlobalTypeRegistered ¶ added in v3.0.3
IsGlobalTypeRegistered checks if a type is registered globally.
func RegisterGlobalType ¶ added in v3.0.3
func RegisterGlobalType(name string, factory TypeValidatorFactory)
RegisterGlobalType registers a type in the global registry.
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" // Generic modifiers KOmitempty Kind = "omitempty" // 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 TypeRegistry ¶ added in v3.0.3
type TypeRegistry struct {
// contains filtered or unexported fields
}
TypeRegistry manages custom type validators and provides a way to register and retrieve type validators without the core engine knowing about specific types.
func NewTypeRegistry ¶ added in v3.0.3
func NewTypeRegistry() *TypeRegistry
NewTypeRegistry creates a new type registry.
func (*TypeRegistry) GetSupportedTypes ¶ added in v3.0.3
func (r *TypeRegistry) GetSupportedTypes() []string
GetSupportedTypes returns a list of all registered custom types.
func (*TypeRegistry) GetTypeValidator ¶ added in v3.0.3
func (r *TypeRegistry) GetTypeValidator(name string, translator translator.Translator) (TypeValidator, bool)
GetTypeValidator creates a new type validator instance for the given type.
func (*TypeRegistry) IsTypeRegistered ¶ added in v3.0.3
func (r *TypeRegistry) IsTypeRegistered(name string) bool
IsTypeRegistered checks if a type is registered.
func (*TypeRegistry) RegisterType ¶ added in v3.0.3
func (r *TypeRegistry) RegisterType(name string, factory TypeValidatorFactory)
RegisterType registers a type validator factory for a given type name.
type TypeValidator ¶ added in v3.0.3
type TypeValidator interface {
// Validate validates a value of this custom type.
Validate(value any) error
}
TypeValidator defines the interface for custom type validators. Custom types must implement this interface to be registered.
func GetGlobalTypeValidator ¶ added in v3.0.3
func GetGlobalTypeValidator(name string, translator translator.Translator) (TypeValidator, bool)
GetGlobalTypeValidator gets a type validator from the global registry.
type TypeValidatorFactory ¶ added in v3.0.3
type TypeValidatorFactory interface {
// CreateValidator creates a new validator instance for this type.
CreateValidator(translator translator.Translator) TypeValidator
}
TypeValidatorFactory creates type validators for a specific custom type.
type ValidatorFunc ¶
ValidatorFunc represents a compiled validation function.