configvalidate

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package configvalidate provides shared primitives for the `validate` subcommands across vendors: an error collector, line/column resolution from a JSON decoder offset, exit-code conventions, and a tiny Levenshtein helper for closest-match suggestions on unknown enum values (event names, hook types, etc.).

Per-vendor rule sets live in cmd/<vendor>/validate.go.

Index

Constants

View Source
const (
	ExitOK         = 0
	ExitErrors     = 1
	ExitUsageError = 2
)

Exit codes per the validate contract:

  • 0: all files passed.
  • 1: at least one validation error reported.
  • 2: usage error (no flags supplied, file not found, etc.).

Variables

View Source
var ErrUsage = errors.New("validate: usage error")

ErrUsage is the sentinel for misuse (no inputs, bad flag values). The caller exits with ExitUsageError and prints usage. Validation rule failures do not use this — they're aggregated into a Collector.

Functions

func ContainsStr

func ContainsStr(haystack []string, needle string) bool

ContainsStr is a small membership helper shared across vendor validate packages. Wraps slices.Contains so callers stay readable at the rule-check sites.

func ExitCode

func ExitCode(c *Collector, usageErr error) int

ExitCode maps the collected issues + caller error to the documented exit codes. usageErr is non-nil when the invocation failed before validation could start (e.g. ErrUsage, file-not-found).

func LineColAt

func LineColAt(data []byte, offset int64) (int, int)

LineColAt converts a byte offset within data (typically the Offset field on a json.SyntaxError or json.UnmarshalTypeError) into a 1-indexed (line, col). Treats \n as a line break; tabs count as one column. Returns (1, 1) when the offset is at the start.

func Suggest

func Suggest(input string, valid []string) string

Suggest returns a short hint for an unknown enum value. When the closest valid value is within editDistanceThreshold (max(2, len/3)), the hint is `did you mean "<closest>"?`; otherwise it falls back to listing all valid values comma-separated. valid must be non-empty.

Types

type Collector

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

Collector accumulates Issues from multiple files in a single run. Per the issue spec, the validate subcommand prints every issue rather than first-fail; the caller drives ordering by appending in vendor-specific traversal order. Print sorts by (path, line, col, msg) so output is deterministic.

func (*Collector) Add

func (c *Collector) Add(i Issue)

Add records an issue. Empty Path / Msg is allowed (caller wraps).

func (*Collector) Addf

func (c *Collector) Addf(path string, line int, format string, args ...any)

Addf is the printf shorthand for Add — convenient when wiring rules that build the message inline.

func (*Collector) Len

func (c *Collector) Len() int

Len reports how many issues have been accumulated.

func (*Collector) Print

func (c *Collector) Print(w io.Writer) int

Print writes every issue to w, sorted deterministically. Returns the number of issues printed.

type Issue

type Issue struct {
	Path string
	Line int
	Col  int
	Msg  string
}

Issue is one problem found in a config file. Line/Col are 1-indexed and may both be zero when the decoder doesn't surface position info (TOML's BurntSushi decoder, primarily). Format: `<Path>:<Line>: <Msg>` when Line > 0, otherwise `<Path>: <Msg>`.

func (Issue) Format

func (i Issue) Format() string

Format renders Issue per the documented `<path>[:line]: <message>` shape. Stable across runs so test fixtures can string-compare.

Jump to

Keyboard shortcuts

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