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 CodeStringType = "string.type" CodeStringLength = "string.length" CodeStringMin = "string.min" CodeStringMax = "string.max" CodeStringNonEmpty = "string.nonempty" CodeStringPattern = "string.pattern" CodeStringOneOf = "string.oneof" CodeStringPrefix = "string.prefix" CodeStringSuffix = "string.suffix" CodeStringURL = "string.url" CodeStringHost = "string.hostname" CodeStringRegexInvalidPattern = "string.regex.invalidPattern" CodeStringRegexInputTooLong = "string.regex.inputTooLong" CodeStringRegexNoMatch = "string.regex.noMatch" CodeStringMinRunes = "string.minRunes" CodeStringMaxRunes = "string.maxRunes" // Number (covers ints and floats) CodeIntType = "int.type" CodeInt64Type = "int64.type" CodeIntMin = "int.min" CodeIntMax = "int.max" CodeNumberMin = "number.min" CodeNumberMax = "number.max" CodeNumberPositive = "number.positive" CodeNumberNonNeg = "number.nonnegative" CodeNumberBetween = "number.between" // Slice CodeSliceType = "slice.type" CodeSliceLength = "slice.length" CodeSliceMin = "slice.min" CodeSliceMax = "slice.max" CodeSliceForEach = "slice.forEach" CodeSliceUnique = "slice.unique" CodeSliceContains = "slice.contains" // Map CodeMapMinKeys = "map.minkeys" CodeMapMaxKeys = "map.maxkeys" // Bool CodeBoolType = "bool.type" // 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.
This type represents a collection of validation errors that can be used as a single error value.
func Join ¶
Join concatenates multiple error values into an Errors slice. It flattens nested Errors and ignores nils.
Parameters:
- errs: Variable number of error values to join.
Returns:
- Errors: A new Errors collection containing all non-nil errors.
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).
Returns:
- map[string][]FieldError: A map where keys are field paths and values are slices of errors for that path.
func (Errors) Error ¶
Error joins all error strings into one compact line.
Returns:
- string: A single line containing all error messages.
func (Errors) Filter ¶
Filter returns errors whose Path has the given prefix. Useful for forms where fields are grouped, e.g. "User.Addresses".
Parameters:
- prefix: The path prefix to filter by.
Returns:
- Errors: A new Errors collection containing only matching errors.
func (Errors) Has ¶
Has reports whether any error targets the exact path.
Parameters:
- path: The field path to check for errors.
Returns:
- bool: True if any error exists for the given path.
func (Errors) MarshalJSON ¶
MarshalJSON ensures deterministic key ordering for better diffs.
Returns:
- []byte: JSON representation of the errors.
- error: An error if JSON marshaling fails.
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"
Fields:
- Path: The field path where the validation failed.
- Code: Stable machine-readable identifier (e.g., "string.min", "int.max").
- Param: Rule parameter (e.g., 3 for min length).
- Msg: Translated, human-readable message if a Translator is set.
func (FieldError) String ¶
func (e FieldError) String() string
String returns a concise string for logs.
Returns:
- string: A formatted string representation of the field error.