core

package
v3.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2026 License: MIT Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasFuncArgs

func HasFuncArgs(rules []types.Rule) bool

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

func SerializeRules(rules []types.Rule) string

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

type CheckFunc func(v any) error

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

type CheckFuncCtx func(ctx context.Context, v any) error

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 NewEngine

func NewEngine() *Engine

NewEngine creates a new Engine with sane defaults.

func NewEngineWithCustomRules

func NewEngineWithCustomRules(custom map[string]func(any) error) *Engine

NewEngineWithCustomRules seeds the engine with custom rules.

func (*Engine) CompileRules

func (e *Engine) CompileRules(rules []types.Rule) func(any) error

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

func (e *Engine) CompileRulesContextE(rules []types.Rule) (types.ContextValidatorFunc, error)

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

func (e *Engine) CompileRulesE(rules []types.Rule) (func(any) error, error)

CompileRulesE compiles AST rules and returns compile-time custom-rule errors.

func (*Engine) CompileRulesWithOpts added in v3.0.5

func (e *Engine) CompileRulesWithOpts(rules []types.Rule, opts types.CompileOpts) func(any) error

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

func (e *Engine) Copy() *Engine

Copy returns a new Engine with the same configuration but separate cache. This mirrors prior behavior used in tests.

func (*Engine) FromRules

func (e *Engine) FromRules(tokens []string) (func(any) error, error)

FromRules compiles validators from rule tokens (e.g. "string","min=2").

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

func (e *Engine) GetPathSeparator() string

GetPathSeparator exposes the configured path separator.

func (*Engine) PathSeparator

func (e *Engine) PathSeparator(sep string) *Engine

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

func (e *Engine) WithContextRuleCompiler(kind types.Kind, rc types.ContextRuleCompiler) *Engine

WithContextRuleCompiler returns a new Engine with a per-instance context-aware rule compiler.

func (*Engine) WithCustomRule

func (e *Engine) WithCustomRule(name string, rule func(any) error) *Engine

WithCustomRule returns a new Engine with the rule registered.

func (*Engine) WithRuleCompiler added in v3.0.5

func (e *Engine) WithRuleCompiler(kind types.Kind, rc types.RuleCompiler) *Engine

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

type RuleBuilder interface {
	GetRules() []types.Rule
	SetRules(rules []types.Rule)
}

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.

func New

func New() *Validate

New returns a new Validate (Engine) with sane defaults.

func NewWithCustomRules

func NewWithCustomRules(custom map[string]func(any) error) *Validate

NewWithCustomRules returns a new Validate (Engine) with custom rules.

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

type ValidatorBuilder interface {
	Build() func(any) error
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL