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 ¶
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 ¶
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) Filter ¶
Filter returns errors whose Path has the given prefix. Useful for forms where fields are grouped, e.g. "User.Addresses".
func (Errors) MarshalJSON ¶
MarshalJSON ensures deterministic key ordering for better diffs.
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.