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

validation

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package validation wraps go-playground/validator/v10 and translates its FieldError slice into kernel/errors.FieldError values, producing a *errors.Error with Kind=KindValidation. It is the "shape validation" half of the two-layer validation strategy described in docs/blueprint/04 §5: struct-tag checks live here; domain / cross-field / rule-engine logic lives in module domain/validation.go and returns errors.E(KindValidation|KindRuleViolation…).

Import boundary: stdlib + kernel/errors + validator lib. Never module, app, adapters, or testkit.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator wraps a *validator.Validate instance. It is safe for concurrent use: go-playground/validator guarantees the *Validate value is read-only after construction, making concurrent Struct calls safe without additional synchronisation.

func New

func New() *Validator

New constructs a Validator ready for use. It registers a TagNameFunc so that field paths in errors.FieldError values use the json tag name (e.g. "email_address") rather than the Go struct field name ("EmailAddress"). Fields without a json tag fall back to their Go field name.

func (*Validator) Struct

func (vl *Validator) Struct(s any) error

Struct validates the exported fields of s using struct tags. On success it returns nil. On failure it returns a *errors.Error with Kind=KindValidation carrying one errors.FieldError per invalid field — all violations are collected (not short-circuited) so the caller receives the full picture in one round-trip.

If s is not a struct (or pointer-to-struct), Struct returns a KindInternal error: passing a non-struct is a programming mistake in the caller, not a user-input problem.

func (*Validator) StructCtx added in v1.1.0

func (vl *Validator) StructCtx(ctx context.Context, s any) error

StructCtx validates s exactly like Struct but localizes each field message against the i18n catalog bound to ctx (kernel/i18n; set by the httpx.Locale middleware). The Field path and machine Code stay byte-stable regardless of locale — only the human Message is translated. With no catalog in ctx (zero-config) the output is byte-identical to Struct's English messages.

httpx.BindAndValidate uses this so API validation errors localize automatically; direct Struct callers keep the English behavior.

Jump to

Keyboard shortcuts

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