validation

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

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 IsValidEmail ¶

func IsValidEmail(email string) bool

func IsValidUUID ¶

func IsValidUUID(id string) bool

func New ¶

func New() action.AnyAction

New creates an action plugin using Copy-on-Write cache reads on the hot path.

func RegisterCustom ¶

func RegisterCustom(tag string, fn validator.FuncCtx)

RegisterCustom registers context-aware validation rules safely at cold-path init.

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.

Directories ¶

Path Synopsis
examples

Jump to

Keyboard shortcuts

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