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) CompileRulesContext(rules []types.Rule) types.ContextValidatorFunc
- func (e *Engine) CompileRulesContextE(rules []types.Rule) (types.ContextValidatorFunc, error)
- func (e *Engine) CompileRulesContextWithOpts(rules []types.Rule, opts types.CompileOpts) types.ContextValidatorFunc
- func (e *Engine) CompileRulesContextWithOptsE(rules []types.Rule, opts types.CompileOpts) (types.ContextValidatorFunc, error)
- func (e *Engine) CompileRulesE(rules []types.Rule) (func(any) error, error)
- func (e *Engine) CompileRulesWithOpts(rules []types.Rule, opts types.CompileOpts) func(any) error
- func (e *Engine) CompileRulesWithOptsE(rules []types.Rule, opts types.CompileOpts) (func(any) error, error)
- func (e *Engine) Copy() *Engine
- func (e *Engine) FromRules(tokens []string) (func(any) error, error)
- func (e *Engine) FromRulesContext(tokens []string) (types.ContextValidatorFunc, error)
- func (e *Engine) FromRulesContextWithOpts(tokens []string, opts types.CompileOpts) (types.ContextValidatorFunc, error)
- func (e *Engine) FromRulesWithOpts(tokens []string, opts types.CompileOpts) (func(any) error, error)
- func (e *Engine) GetPathSeparator() string
- func (e *Engine) PathSeparator(sep string) *Engine
- func (e *Engine) StructRuleCompiler(kind types.Kind) (StructRuleCompiler, bool)
- func (e *Engine) Translator() translator.Translator
- func (e *Engine) WithContextRuleCompiler(kind types.Kind, rc types.ContextRuleCompiler) *Engine
- func (e *Engine) WithCustomRule(name string, rule func(any) error) *Engine
- func (e *Engine) WithRuleCompiler(kind types.Kind, rc types.RuleCompiler) *Engine
- func (e *Engine) WithStructRuleCompiler(kind types.Kind, compiler StructRuleCompiler) *Engine
- func (e *Engine) WithTranslator(t translator.Translator) *Engine
- func (e *Engine) WithTypeValidator(name string, factory types.TypeValidatorFactory) *Engine
- type RuleBuilder
- type StructRuleCompiler
- type StructRuleContext
- type StructRuleFunc
- 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) CompileRulesContext ¶ added in v3.0.5
func (e *Engine) CompileRulesContext(rules []types.Rule) types.ContextValidatorFunc
CompileRulesContext compiles AST rules into a context-aware validator.
func (*Engine) CompileRulesContextE ¶ added in v3.0.5
CompileRulesContextE compiles AST rules into a context-aware validator and returns compile errors.
func (*Engine) CompileRulesContextWithOpts ¶ added in v3.0.5
func (e *Engine) CompileRulesContextWithOpts(rules []types.Rule, opts types.CompileOpts) types.ContextValidatorFunc
CompileRulesContextWithOpts compiles AST rules into a context-aware validator with options.
func (*Engine) CompileRulesContextWithOptsE ¶ added in v3.0.5
func (e *Engine) CompileRulesContextWithOptsE(rules []types.Rule, opts types.CompileOpts) (types.ContextValidatorFunc, error)
CompileRulesContextWithOptsE compiles AST rules into a context-aware validator with options and returns compile errors.
func (*Engine) CompileRulesE ¶ added in v3.0.5
CompileRulesE compiles AST rules and returns compile-time custom-rule errors.
func (*Engine) CompileRulesWithOpts ¶ added in v3.0.5
CompileRulesWithOpts compiles AST rules with options.
func (*Engine) CompileRulesWithOptsE ¶ added in v3.0.5
func (e *Engine) CompileRulesWithOptsE(rules []types.Rule, opts types.CompileOpts) (func(any) error, error)
CompileRulesWithOptsE compiles AST rules with options and returns compile errors.
func (*Engine) Copy ¶
Copy returns a new Engine with the same configuration but separate cache. This mirrors prior behavior used in tests.
func (*Engine) FromRulesContext ¶ added in v3.0.5
func (e *Engine) FromRulesContext(tokens []string) (types.ContextValidatorFunc, error)
FromRulesContext compiles a context-aware validator from rule tokens.
func (*Engine) FromRulesContextWithOpts ¶ added in v3.0.5
func (e *Engine) FromRulesContextWithOpts(tokens []string, opts types.CompileOpts) (types.ContextValidatorFunc, error)
FromRulesContextWithOpts compiles a context-aware validator from rule tokens with compile options.
func (*Engine) FromRulesWithOpts ¶ added in v3.0.5
func (e *Engine) FromRulesWithOpts(tokens []string, opts types.CompileOpts) (func(any) error, error)
FromRulesWithOpts compiles validators from rule tokens with compile options.
func (*Engine) GetPathSeparator ¶
GetPathSeparator exposes the configured path separator.
func (*Engine) PathSeparator ¶
PathSeparator returns a new Engine with a different path separator.
func (*Engine) StructRuleCompiler ¶ added in v3.0.5
func (e *Engine) StructRuleCompiler(kind types.Kind) (StructRuleCompiler, bool)
StructRuleCompiler returns a registered per-instance struct rule compiler.
func (*Engine) Translator ¶
func (e *Engine) Translator() translator.Translator
Translator exposes the configured translator.
func (*Engine) WithContextRuleCompiler ¶ added in v3.0.5
WithContextRuleCompiler returns a new Engine with a per-instance context-aware rule compiler.
func (*Engine) WithCustomRule ¶
WithCustomRule returns a new Engine with the rule registered.
func (*Engine) WithRuleCompiler ¶ added in v3.0.5
WithRuleCompiler returns a new Engine with a per-instance rule compiler.
func (*Engine) WithStructRuleCompiler ¶ added in v3.0.5
func (e *Engine) WithStructRuleCompiler(kind types.Kind, compiler StructRuleCompiler) *Engine
WithStructRuleCompiler returns a new Engine with a per-instance struct rule compiler.
func (*Engine) WithTranslator ¶
func (e *Engine) WithTranslator(t translator.Translator) *Engine
WithTranslator returns a new Engine with a translator.
func (*Engine) WithTypeValidator ¶ added in v3.0.5
func (e *Engine) WithTypeValidator(name string, factory types.TypeValidatorFactory) *Engine
WithTypeValidator returns a new Engine with a per-instance custom type validator.
type RuleBuilder ¶
RuleBuilder defines the interface for building rules. This allows validators to construct rules that the core engine can compile.
type StructRuleCompiler ¶ added in v3.0.5
type StructRuleCompiler func(types.Rule) (StructRuleFunc, error)
StructRuleCompiler compiles a struct-level rule before validation.
type StructRuleContext ¶ added in v3.0.5
type StructRuleContext struct {
Path string
Field reflect.StructField
Value any
Owner reflect.Value
Rule types.Rule
Context context.Context
Translator translator.Translator
}
StructRuleContext is the runtime input for struct-level field rules.
func (StructRuleContext) FieldValue ¶ added in v3.0.5
func (ctx StructRuleContext) FieldValue(name string) (any, bool)
FieldValue returns an exported same-level field value by Go field name.
type StructRuleFunc ¶ added in v3.0.5
type StructRuleFunc func(StructRuleContext) error
StructRuleFunc validates one struct field with access to same-level fields.
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 ¶
type ValidateOpts struct {
StopOnFirst bool
CollectAllRules bool
PathSep string
FieldNameFunc func(reflect.StructField) string
}
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.