types

package
v3.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2025 License: MIT Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

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.

func (*Compiler) T

func (c *Compiler) T(code string, defaultMsg string, params []any) string

T returns a translated message for the given code, or defaultMsg if no translator or translation is available. This is a public proxy to enable external validator plugins to use the compiler's translator.

type FieldValidator

type FieldValidator func(field any) error

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"

	// 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 NewRule

func NewRule(kind Kind, args map[string]any) Rule

NewRule creates a new rule with the given kind and arguments.

func NewRuleWithElem

func NewRuleWithElem(kind Kind, args map[string]any, elem *Rule) Rule

NewRuleWithElem builds a Rule with an element sub-rule for nesting.

func NewRuleWithElemValue deprecated

func NewRuleWithElemValue(kind Kind, args map[string]any, elem Rule) Rule

Deprecated: use NewRuleWithElem(kind, args, &elem) directly when you need to pass a value. This variant was kept for compatibility and will be removed in a future version.

func ParseTag

func ParseTag(tag string) ([]Rule, error)

Example: "string;min=3;max=50" -> []Rule

type RuleCompiler

type RuleCompiler func(c *Compiler, rule Rule) (func(any) error, error)

RuleCompiler compiles a single Rule into a validate function. Implementations may precompute heavy state (e.g., compiled regex).

type ValidatorFunc

type ValidatorFunc func(v any) error

ValidatorFunc represents a compiled validation function.

Jump to

Keyboard shortcuts

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