validate

package module
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: 7 Imported by: 0

README

validate

Composable validation for Go with fluent builders, rule tags, struct validation, and optional message translation.

Packages

  • github.com/aatuh/validate/v3: main API (Validate, builders, FromTag)
  • github.com/aatuh/validate/v3/core: generic validation engine (unified, cache-optimized)
  • github.com/aatuh/validate/v3/glue: integration layer with builders
  • github.com/aatuh/validate/v3/validators: type-specific rules
  • github.com/aatuh/validate/v3/errors: error types and codes
  • github.com/aatuh/validate/v3/structvalidator: struct validation
  • github.com/aatuh/validate/v3/translator: i18n helpers

The package is architecturally separated into:

  • Core: Generic validation engine, type-agnostic, with unified caching
  • Glue: Integration layer that connects core with type-specific builders
  • Validators: Type-specific validation implementations

Quick start

For examples, see the examples package.

Run examples:

# Run all examples in learning order
go test ./examples -v -count 1

# Run specific examples
go test ./examples -v -count 1 -run Test_structTags
Supported data types
Type Description
string UTF-8 strings; rune-aware length rules
int Any Go int type accepted at call time
int64 Requires exactly int64 at call time
slice Any slice type; supports per-element
bool Boolean type-check only

Plugin rules that operate on strings (imported by default):

  • uuid: validates canonical UUID string
  • ulid: validates ULID string
  • email: validates email address string

Note on plugin registration:

  • validate.New() already registers these plugin validators via blank imports inside the root package.
  • If you use core/glue directly, blank-import the plugins yourself:
import (
    _ "github.com/aatuh/validate/v3/validators/email"
    _ "github.com/aatuh/validate/v3/validators/ulid"
    _ "github.com/aatuh/validate/v3/validators/uuid"
)
Tags by type

Tags can be used in struct field tags or with FromTag.

string
Tag Meaning
len=N Exact byte length equals N
min=N Minimum byte length
max=N Maximum byte length
minRunes=N Minimum Unicode rune count
maxRunes=N Maximum Unicode rune count
oneof=a,b,c Value must be one of the listed values
regex=PATTERN Full-match against PATTERN (anchors added)
uuid UUID format (via plugin)
ulid ULID format (via plugin)
email Email format (via plugin)

Notes:

  • oneof supports comma or space separated values.
  • Regex is made safe (anchors added, input length capped).
int / int64
Tag Meaning
min=N Minimum numeric value
max=N Maximum numeric value

Use int64;... to require exactly int64 at validation time; int;... accepts any Go int type (int, int8, int16, int32, int64, and unsigned variants for base type checks).

slice
Tag Meaning
len=N Exact slice length
min=N Minimum slice length
max=N Maximum slice length
foreach=(...) Element rules in parentheses, e.g. string rules

Example: slice;min=1;foreach=(string;min=2).

bool
Tag Meaning
bool Type check (boolean)

Error codes

Stable constants for programmatic handling (see errors/codes.go):

const (
    // String
    CodeStringMin = "string.min"
    CodeStringMax = "string.max"
    CodeStringNonEmpty = "string.nonempty"
    CodeStringPattern = "string.pattern"
    CodeStringOneOf = "string.oneof"

    // Number (ints/floats)
    CodeNumberMin = "number.min"
    CodeNumberMax = "number.max"

    // Slice
    CodeSliceMin = "slice.min"
    CodeSliceMax = "slice.max"
)

Documentation

Overview

Package validate provides composable validation helpers for Go with fluent builders, rule tags, and struct validation. It includes optional message translation support.

Index

Constants

View Source
const (
	// String validation kinds
	KString    = types.KString
	KLength    = types.KLength
	KMinLength = types.KMinLength
	KMaxLength = types.KMaxLength
	KRegex     = types.KRegex
	KOneOf     = types.KOneOf
	KMinRunes  = types.KMinRunes
	KMaxRunes  = types.KMaxRunes

	// Generic modifiers
	KOmitempty = types.KOmitempty

	// Integer validation kinds
	KInt    = types.KInt
	KInt64  = types.KInt64
	KMinInt = types.KMinInt
	KMaxInt = types.KMaxInt

	// Slice validation kinds
	KSlice          = types.KSlice
	KSliceLength    = types.KSliceLength
	KMinSliceLength = types.KMinSliceLength
	KMaxSliceLength = types.KMaxSliceLength
	KForEach        = types.KForEach

	// Boolean validation kinds
	KBool = types.KBool
)

Re-export commonly used rule kinds

Variables

View Source
var (
	NewSimpleTranslator        = translator.NewSimpleTranslator
	DefaultEnglishTranslations = translator.DefaultEnglishTranslations
)

Re-export translator functions

View Source
var (
	NewRule = types.NewRule
)

Re-export types functions

Functions

func FromTag

func FromTag(v *Validate, tag string) (func(any) error, error)

FromTag compiles a single tag string using v (or a fresh instance).

func ValidateStruct added in v3.0.3

func ValidateStruct(v *Validate, s any) error

ValidateStruct validates a struct using v (or a fresh instance).

Types

type BoolBuilder

type BoolBuilder = glue.BoolBuilder

type CustomTypeBuilder added in v3.0.3

type CustomTypeBuilder = glue.CustomTypeBuilder

type Errors added in v3.0.3

type Errors = errors.Errors

type IntBuilder

type IntBuilder = glue.IntBuilder

type Kind added in v3.0.4

type Kind = types.Kind

type Rule added in v3.0.4

type Rule = types.Rule

Re-export types package for manual rule construction

type SimpleTranslator added in v3.0.4

type SimpleTranslator = translator.SimpleTranslator

type SliceBuilder

type SliceBuilder = glue.SliceBuilder

type StringBuilder

type StringBuilder = glue.StringBuilder

type Translator added in v3.0.4

type Translator = translator.Translator

Re-export translator package

type Validate

type Validate = glue.Validate

Re-export types for a developer-friendly root facade.

func New

func New() *Validate

New returns a Validate configured with sensible defaults.

Defaults: - Installs default English translations via SimpleTranslator. - Registers built-in plugins (email, ulid, uuid) via blank imports.

func NewBare

func NewBare() *Validate

NewBare returns a Validate without installing a default translator. Useful for advanced setups that manage translations differently.

func NewWithTranslator

func NewWithTranslator(tr translator.Translator) *Validate

NewWithTranslator returns a Validate configured with the provided translator while keeping other defaults.

type ValidatorFunc added in v3.0.4

type ValidatorFunc = types.ValidatorFunc

Directories

Path Synopsis
Package core provides the main validation engine and fluent builder APIs.
Package core provides the main validation engine and fluent builder APIs.
Package errors provides error types and error handling utilities for validation.
Package errors provides error types and error handling utilities for validation.
Package examples provides examples for the validate package.
Package examples provides examples for the validate package.
Package structvalidator provides struct validation functionality using reflection.
Package structvalidator provides struct validation functionality using reflection.
Package translator provides internationalization support for validation messages.
Package translator provides internationalization support for validation messages.
Package types provides the core validation engine types and rule system.
Package types provides the core validation engine types and rule system.
Package validators provides built-in validation rules and plugin architecture.
Package validators provides built-in validation rules and plugin architecture.
email
Package email provides email address validation as a plugin.
Package email provides email address validation as a plugin.
ulid
Package ulid provides ULID validation as a plugin.
Package ulid provides ULID validation as a plugin.
uuid
Package uuid provides UUID validation as a plugin.
Package uuid provides UUID validation as a plugin.

Jump to

Keyboard shortcuts

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