Documentation
¶
Overview ¶
Package core provides the main validation engine and fluent builder APIs.
The core package contains the primary validation functionality including the main Validate struct, fluent builder APIs for constructing validators, tag-based validation rule parsing, and performance optimizations through caching. It provides thread-safe validation with immutable configuration and comprehensive caching for optimal performance.
Package core holds builder utilities for the validate library.
Index ¶
- func HasFuncArgs(rules []types.Rule) bool
- func SerializeRules(rules []types.Rule) string
- type CheckFunc
- type CheckFuncCtx
- type Engine
- func (e *Engine) CompileRules(rules []types.Rule) func(any) error
- func (e *Engine) Copy() *Engine
- func (e *Engine) FromRules(tokens []string) (func(any) error, error)
- func (e *Engine) GetPathSeparator() string
- func (e *Engine) PathSeparator(sep string) *Engine
- func (e *Engine) Translator() translator.Translator
- func (e *Engine) WithCustomRule(name string, rule func(any) error) *Engine
- func (e *Engine) WithTranslator(t translator.Translator) *Engine
- type RuleBuilder
- type Validate
- type ValidateOpts
- type ValidatorBuilder
- type ValidatorFactory
- type ValidatorRegistry
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HasFuncArgs ¶
HasFuncArgs returns true if any rule argument is a function (directly or nested inside maps/slices). This is used to decide whether to skip caching, because function pointer addresses are not deterministic.
func SerializeRules ¶
SerializeRules returns a deterministic, canonical string for a rule set. Use it as a cache key for compiled validators. It avoids embedding function addresses (which are process-specific and non-deterministic) by emitting a stable "fn" marker for function arguments.
Types ¶
type CheckFunc ¶
CheckFunc validates a single value and returns an error if invalid.
func WithoutContext ¶
func WithoutContext(f CheckFuncCtx) CheckFunc
WithoutContext adapts a CheckFuncCtx to a CheckFunc.
type CheckFuncCtx ¶
CheckFuncCtx is the context-aware variant of CheckFunc.
func WithContext ¶
func WithContext(f CheckFunc) CheckFuncCtx
WithContext adapts a CheckFunc to a CheckFuncCtx that ignores ctx.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is the generic validation engine. It compiles tag tokens or AST rules into reusable validator functions and caches the results.
func NewEngineWithCustomRules ¶
NewEngineWithCustomRules seeds the engine with custom rules.
func (*Engine) CompileRules ¶
CompileRules compiles AST rules. We cache deterministically unless any rule carries a function argument (non-deterministic).
func (*Engine) Copy ¶
Copy returns a new Engine with the same configuration but separate cache. This mirrors prior behavior used in tests.
func (*Engine) GetPathSeparator ¶
GetPathSeparator exposes the configured path separator.
func (*Engine) PathSeparator ¶
PathSeparator returns a new Engine with a different path separator.
func (*Engine) Translator ¶
func (e *Engine) Translator() translator.Translator
Translator exposes the configured translator.
func (*Engine) WithCustomRule ¶
WithCustomRule returns a new Engine with the rule registered.
func (*Engine) WithTranslator ¶
func (e *Engine) WithTranslator(t translator.Translator) *Engine
WithTranslator returns a new Engine with a translator.
type RuleBuilder ¶
RuleBuilder defines the interface for building rules. This allows validators to construct rules that the core engine can compile.
type Validate ¶
type Validate = Engine
Validate is now an alias of Engine to avoid duplication while keeping the public API stable for existing imports and tests.
type ValidateOpts ¶
ValidateOpts tunes validation behavior per call.
func ApplyOpts ¶
func ApplyOpts(v *Validate, o ValidateOpts) ValidateOpts
ApplyOpts fills missing values using the given *Validate instance.
func (ValidateOpts) WithDefaults ¶
func (o ValidateOpts) WithDefaults() ValidateOpts
WithDefaults keeps the door open for future defaults.
type ValidatorBuilder ¶
ValidatorBuilder defines the interface for building validators. This allows the core engine to work with any validator implementation without knowing the specific types.
type ValidatorFactory ¶
type ValidatorFactory interface {
CreateValidator(validatorType string) ValidatorBuilder
}
ValidatorFactory defines the interface for creating validator instances. This allows the core engine to create validators without knowing their specific implementation details.
type ValidatorRegistry ¶
type ValidatorRegistry interface {
RegisterValidator(name string, factory func() ValidatorBuilder)
GetValidator(name string) (ValidatorBuilder, bool)
}
ValidatorRegistry defines the interface for registering custom validators. This allows the core engine to work with plugin-style validators.