validationerror

package
v0.88.1 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 3 Imported by: 0

README

validationerror Package

Shared payload and formatting helpers for structured validation errors.

Overview

The validationerror package centralizes field, value, reason, and suggestion details for validation errors. Packages such as parser and workflow embed Payload in their concrete error types, allowing callers to inspect them uniformly with errors.As and ValidationError.

Public API

Types
Type Description
Payload Validation details containing the exported Field, Value, Reason, and Suggestion string fields
ValidationError Interface for errors exposing ValidationField, ValidationValue, ValidationReason, and ValidationSuggestion
Constants
Constant Type Value Description
MaxValueLength int 100 Maximum value length used by Format when truncation is enabled
Functions and methods
Function Signature Description
Format func Format(header string, p Payload, truncateValue bool) string Formats a header and payload as a deterministic multi-line message
Payload.ValidationField func (p Payload) ValidationField() string Returns the field that failed validation
Payload.ValidationValue func (p Payload) ValidationValue() string Returns the offending value, if present
Payload.ValidationReason func (p Payload) ValidationReason() string Returns the reason validation failed
Payload.ValidationSuggestion func (p Payload) ValidationSuggestion() string Returns remediation guidance, if present

Format always includes the header and reason. It includes the value and suggestion only when they are non-empty. When truncateValue is true, the value is truncated to MaxValueLength.

Usage Example

var validationErr validationerror.ValidationError
if errors.As(err, &validationErr) {
    fmt.Printf("%s: %s\n", validationErr.ValidationField(), validationErr.ValidationReason())
}

Dependencies

Internal:

  • pkg/stringutil — value truncation

External:

  • None beyond the Go standard library.

This specification is automatically maintained by the spec-extractor workflow.

Documentation

Overview

Package validationerror provides a shared, dependency-neutral payload and message formatting for structured field/value/reason/suggestion validation errors. Packages such as parser and workflow embed Payload in their own error types so that:

  • the message formatting logic (including value truncation) lives in one place and future fixes don't need to be duplicated per package, and
  • callers can uniformly detect any structured validation error with errors.As(err, &validationerror.ValidationError) regardless of which package produced it.

Index

Constants

View Source
const MaxValueLength = 100

MaxValueLength is the maximum number of characters kept for the Value field when formatting a message with value truncation enabled.

Variables

This section is empty.

Functions

func Format

func Format(header string, p Payload, truncateValue bool) string

Format renders header followed by the payload's value/reason/suggestion in a deterministic multi-line shape. When truncateValue is true, Value is truncated to MaxValueLength before being included.

Types

type Payload

type Payload struct {
	Field      string
	Value      string
	Reason     string
	Suggestion string
}

Payload holds the common field/value/reason/suggestion data shared by structured validation errors across packages.

func (Payload) ValidationField

func (p Payload) ValidationField() string

ValidationField returns the name of the field that failed validation.

func (Payload) ValidationReason

func (p Payload) ValidationReason() string

ValidationReason returns the reason validation failed.

func (Payload) ValidationSuggestion

func (p Payload) ValidationSuggestion() string

ValidationSuggestion returns actionable remediation guidance, if any.

func (Payload) ValidationValue

func (p Payload) ValidationValue() string

ValidationValue returns the offending value, if any.

type ValidationError

type ValidationError interface {
	error
	ValidationField() string
	ValidationValue() string
	ValidationReason() string
	ValidationSuggestion() string
}

ValidationError is the common interface implemented by structured field/value/reason/suggestion validation errors across packages (for example parser.ValidationError and workflow.WorkflowValidationError). Use errors.As(err, &target) with a variable of this interface type to uniformly extract validation details regardless of the concrete type.

Jump to

Keyboard shortcuts

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