Help us shape the future of Go! Take the 2026 Go Developer Survey

validation

package module
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: Apache-2.0 Imports: 11 Imported by: 1

README ¶

nexssp/validation

nexssp/validation provides high-performance struct and field validation for the Nexss ecosystem using github.com/go-playground/validator/v10.

🚀 Usage Patterns

1. Direct Kernel Action Integration (AutoValidate)
validatedAction := validation.AutoValidate(
    action.New("goal.create", handleGoal),
).Build()
2. Direct Builder Method (.Validate(...))
act := action.New("order.process", handleOrder).
    Validate(func(ctx context.Context, req OrderReq) error {
        return validation.Struct(ctx, req)
    }).
    Build()
3. Pipeline Plugin Hook (validation.New())
plugin := validation.New()

for _, act := range registry.Actions() {
    act.AddAnyHook(plugin.(action.Executable).Describe().Hooks[0])
}
4. Standalone Validation
if err := validation.Struct(ctx, myStruct); err != nil {
    return validation.FromValidatorError(err)
}

Extension Paattern

package main

import (
	"context"

	"github.com/go-playground/validator/v10"
	"github.com/nexssp/validation"
)

// Register domain-specific rules at application cold-start
func init() {
	validation.RegisterCustom("nip", func(_ context.Context, fl validator.FieldLevel) bool {
		return isPolishNIPValid(fl.Field().String())
	})
}

type InvoiceDTO struct {
	TaxID string `json:"tax_id" validate:"required,nip"`
}

func isPolishNIPValid(nip string) bool {
	// App-level or domain-specific package implementation
	return true
}

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

func AutoValidate ¶

func AutoValidate[Req, Res any](b *action.Builder[Req, Res]) *action.Builder[Req, Res]

AutoValidate wires Kernel's .Validate(...) builder contract directly to validation.Struct, mapping validator/v10 errors into clean xerr.ValidationDetails.

func CompileWithTimeout ¶

func CompileWithTimeout(pattern string, timeout time.Duration) *regexp.Regexp

CompileWithTimeout prevents ReDoS attacks on dynamic patterns.

func FromValidatorError ¶

func FromValidatorError(err error) *xerr.AppError

FromValidatorError maps validator/v10 field errors to Nexss structured details with readable messages.

func Hook ¶ added in v0.2.0

func Hook() action.AnyHook

Hook returns a stateless validation hook suitable for action.Library.Hooks. The OnBuild predicate drops the hook from every action whose Req type carries no `validate:` tags and does not implement Validatable — such actions pay zero cost in Do().

Usage:

action.MustNewRegistry(action.Library{
    Name:    "myapp",
    Actions: myActions,
    Hooks:   []action.AnyHook{validation.Hook()},
})

func IsValidEmail ¶

func IsValidEmail(email string) bool

func IsValidUUID ¶

func IsValidUUID(id string) bool

func RegisterCustom ¶

func RegisterCustom(tag string, fn validator.FuncCtx)

RegisterCustom registers context-aware validation rules safely at cold-path init. Panics if the tag is invalid or already registered: both are programmer errors.

func RegisterType ¶

func RegisterType(fn validator.CustomTypeFunc, types ...any)

RegisterType registers custom type handlers safely at cold-path init.

func Struct ¶

func Struct(ctx context.Context, value any) error

Struct validates exported fields using `validate:"..."` tags. Automatically trims whitespace, evaluates tags, and calls Validatable.Validate() if implemented.

func TrimStringFields ¶

func TrimStringFields(value any)

TrimStringFields recurses through exported string fields in structs, pointers, and slices, trimming leading and trailing whitespace in-place.

func ValidateAction ¶

func ValidateAction[T any](name string) *action.BuiltAction[T, T]

ValidateAction creates an identity action that validates T using validate tags.

Types ¶

type Validatable ¶

type Validatable interface {
	Validate() error
}

Validatable allows structs to execute custom semantic validation logic after tag validation passes. Implementations must be safe for concurrent use.

Directories ¶

Path Synopsis
examples

Jump to

Keyboard shortcuts

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