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

validator

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package validator provides a small, generic business-rule validator for the service layer — the below-transport-boundary complement to the proto/request validation performed at the transport edge (connect ValidateInterceptor). It carries no backends and needs no Kind/factory: it is a pure composition mechanism over caller-supplied rules.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ensure

func Ensure(dtos ...interfaces.Validatable) error

Ensure runs Validate() on each DTO in order and returns the first coded violation, or nil when all pass. It is the hand-written-service chokepoint: call it at a service or workflow entry to enforce a DTO's self-validation — the generated Validate() composed with its optional domainRules() hook — below the transport edge, so non-edge writers (seed, connector sync, internal RPC) and server-injected fields are validated as CodeInvalidInput just like edge requests.

A nil entry — an untyped nil OR a typed nil pointer (e.g. a nil *SomeDTO passed as an optional Validatable) — is skipped, so an unset optional DTO never panics in Validate(). Callers that must aggregate every violation (e.g. a bulk request reporting all offenders in one response) run each DTO's Validate() themselves rather than short-circuiting here.

Types

type Rule

type Rule[T any] func(*T) error

Rule validates one aspect of a *T, returning a non-nil error on violation and nil when satisfied. Rules should return a core/errors.AppError carrying CodeInvalidInput (ARCHITECTURE.md#error-codes) so the failure classifies and maps to a 400 / InvalidArgument at the transport edge.

type Validator

type Validator[T any] struct {
	// contains filtered or unexported fields
}

Validator runs an ordered set of Rules against a *T, short-circuiting on the first violation. The zero Validator (no rules) accepts everything.

func New

func New[T any](rules ...Rule[T]) *Validator[T]

New builds a Validator from the given rules, evaluated in the order supplied.

func (*Validator[T]) Validate

func (v *Validator[T]) Validate(entity *T) error

Validate runs each rule in order and returns the first violation, or nil if the entity satisfies every rule. Its signature matches the func the service builder's WithValidation accepts, so `WithValidation(v.Validate)` wires it in directly.

Jump to

Keyboard shortcuts

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