validation

package
v0.1.0-preview.4 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package validation provides immutable, source-safe application validation errors for generated form and controller boundaries.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Errors

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

Errors is an immutable ordered validation result.

func Join

func Join(values ...Errors) (Errors, error)

Join returns a new result containing each input in argument order.

func New

func New(violations ...Violation) (Errors, error)

New validates and defensively copies violations in caller order.

func Validate

func Validate[T any](
	ctx context.Context,
	value T,
	validators ...Validator[T],
) (Errors, error)

Validate applies validators in caller order and returns their immutable violations in the same order. It stops on cancellation, an operational error, or the package violation limit.

Example
positive := ValidatorFunc[int](func(
	_ context.Context,
	value int,
) (Errors, error) {
	if value > 0 {
		return Errors{}, nil
	}
	violation, err := Object("positive", "Value must be positive")
	if err != nil {
		return Errors{}, err
	}
	return New(violation)
})
result, err := Validate(context.Background(), 0, positive)
if err != nil {
	fmt.Println("validation failed")
	return
}
fmt.Println(result.All()[0].Code)
Output:
positive

func (Errors) Add

func (validation Errors) Add(
	violations ...Violation,
) (Errors, error)

Add returns a new result containing the existing and additional violations.

func (Errors) All

func (validation Errors) All() []Violation

All returns a defensive copy in stable insertion order.

func (Errors) ForField

func (validation Errors) ForField(field string) []Violation

ForField returns violations for one exact field in insertion order.

func (Errors) Len

func (validation Errors) Len() int

Len returns the number of violations.

func (Errors) Valid

func (validation Errors) Valid() bool

Valid reports whether there are no violations.

type Validator

type Validator[T any] interface {
	Validate(context.Context, T) (Errors, error)
}

Validator performs layer-neutral validation for one exact Go type. Validation failures belong in Errors; returned error values identify operational failures such as cancellation or unavailable dependencies.

type ValidatorFunc

type ValidatorFunc[T any] func(context.Context, T) (Errors, error)

ValidatorFunc adapts an ordinary typed function to Validator.

func (ValidatorFunc[T]) Validate

func (validate ValidatorFunc[T]) Validate(
	ctx context.Context,
	value T,
) (Errors, error)

Validate invokes the adapted function and rejects nil contexts/functions.

type Violation

type Violation struct {
	Field   string
	Code    string
	Message string
}

Violation is one safe field or object validation failure. It intentionally carries no rejected value.

func Field

func Field(field, code, message string) (Violation, error)

Field constructs one field violation.

func Object

func Object(code, message string) (Violation, error)

Object constructs one object-level violation.

Jump to

Keyboard shortcuts

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