errors

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package errors provides error types and error handling utilities for validation.

The errors package defines structured error types that provide detailed information about validation failures, including field paths, error codes, and human-readable messages.

Key types: - FieldError: Represents a single validation failure at a specific field path - Errors: A collection of FieldError that implements the error interface

The package also provides utility functions for error handling, filtering, and conversion to different formats (JSON, maps, etc.).

Index

Constants

View Source
const (
	// Generic
	CodeUnknown   = "unknown"
	CodeRequired  = "required"
	CodeOmitEmpty = "omitempty" // informational when skipped

	// String
	CodeStringMin      = "string.min"
	CodeStringMax      = "string.max"
	CodeStringNonEmpty = "string.nonempty"
	CodeStringPattern  = "string.pattern"
	CodeStringOneOf    = "string.oneof"
	CodeStringPrefix   = "string.prefix"
	CodeStringSuffix   = "string.suffix"
	CodeStringURL      = "string.url"
	CodeStringUUID     = "string.uuid"
	CodeStringHost     = "string.hostname"

	// Number (covers ints and floats)
	CodeNumberMin      = "number.min"
	CodeNumberMax      = "number.max"
	CodeNumberPositive = "number.positive"
	CodeNumberNonNeg   = "number.nonnegative"
	CodeNumberBetween  = "number.between"

	// Slice
	CodeSliceMin      = "slice.min"
	CodeSliceMax      = "slice.max"
	CodeSliceUnique   = "slice.unique"
	CodeSliceContains = "slice.contains"

	// Map
	CodeMapMinKeys = "map.minkeys"
	CodeMapMaxKeys = "map.maxkeys"

	// Time
	CodeTimeNotZero = "time.notzero"
	CodeTimeBefore  = "time.before"
	CodeTimeAfter   = "time.after"
	CodeTimeBetween = "time.between"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Errors

type Errors []FieldError

Errors is a collection of FieldError that implements error.

The Error() message is a single line intended for logs. For structured consumption prefer AsMap or JSON marshaling.

func Join

func Join(errs ...error) Errors

Join concatenates multiple error values into an Errors slice. It flattens nested Errors and ignores nils.

func (Errors) AsMap

func (es Errors) AsMap() map[string][]FieldError

AsMap groups errors by exact field path. The slice per key preserves original order (stable).

func (Errors) Error

func (es Errors) Error() string

Error joins all error strings into one compact line.

func (Errors) Filter

func (es Errors) Filter(prefix string) Errors

Filter returns errors whose Path has the given prefix. Useful for forms where fields are grouped, e.g. "User.Addresses".

func (Errors) Has

func (es Errors) Has(path string) bool

Has reports whether any error targets the exact path.

func (Errors) MarshalJSON

func (es Errors) MarshalJSON() ([]byte, error)

MarshalJSON ensures deterministic key ordering for better diffs.

func (Errors) Sort

func (es Errors) Sort()

SortByPath then Code to provide stable presentation when needed.

func (Errors) Unwrap

func (es Errors) Unwrap() error

Unwrap allows using errors.Is/As when you wrap Errors with fmt.Errorf. Returns nil because there is no single underlying error to unwrap.

type FieldError

type FieldError struct {
	Path string `json:"path"`
	// Code is a stable machine-readable identifier, e.g. "string.min",
	// "int.max", "slice.unique". Prefer stable codes in UIs and tests.
	Code string `json:"code"`
	// Param carries rule parameter, e.g. 3 for min length. Keep it simple.
	Param any `json:"param,omitempty"`
	// Msg is the translated, human-readable message if a Translator is set.
	Msg string `json:"message,omitempty"`
}

FieldError represents one validation failure at a specific path. Path example: "User.Addresses[2].Zip"

func (FieldError) String

func (e FieldError) String() string

String returns a concise string for logs.

Jump to

Keyboard shortcuts

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