core

package
v3.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2025 License: MIT Imports: 9 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) 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) 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) Translator

func (e *Engine) Translator() translator.Translator

Translator exposes the configured translator.

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) WithTranslator

func (e *Engine) WithTranslator(t translator.Translator) *Engine

WithTranslator returns a new Engine with a translator.

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 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
	PathSep     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