validate

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package validate infers validation rules from key name suffixes and runs them against parsed values. The suffix table is also the source of truth for masking, so that `show`, `diff --values`, and `validate` all agree on which keys are sensitive.

Index

Constants

View Source
const Masked = "****"

Masked is the canonical masked representation used across all commands.

Variables

View Source
var Rules = []*Rule{
	{
		Name:     "url",
		Suffixes: []string{"_URL", "_URI"},
		Validate: validateURL,
	},
	{
		Name:     "port",
		Suffixes: []string{"_PORT"},
		Validate: validatePort,
	},
	{
		Name:     "secret",
		Suffixes: []string{"_KEY", "_SECRET", "_TOKEN", "_PASSWORD"},
		Validate: validateNonEmpty,
		Mask:     true,
	},
	{
		Name:     "email",
		Suffixes: []string{"_EMAIL", "_ADDRESS"},
		Validate: validateEmail,
	},
	{
		Name:     "bool",
		Suffixes: []string{"_ENABLED", "_DEBUG", "_FLAG"},
		Validate: validateBool,
	},
}

Rules is the ordered list of known validation rules. The order determines matching priority when a key could in principle match multiple rules; more specific suffixes should appear earlier.

Functions

func CountErrors

func CountErrors(results []Result) int

CountErrors returns the number of results with a non-nil Err.

func ShouldMask

func ShouldMask(key string) bool

ShouldMask reports whether values for the given key should be masked when displayed. It is safe to call with any key; unmatched keys return false.

Types

type Input

type Input struct {
	Key   string
	Value string
}

Input is a single key/value to validate. It is decoupled from parser.Entry so that this package stays independent of the parser.

type Result

type Result struct {
	Key   string
	Value string
	Rule  *Rule // nil when no rule matched
	Err   error // nil when the value passed, or when no rule matched
}

Result is the outcome for one key.

func All

func All(inputs []Input, strict bool) []Result

All runs validation over every input. Keys without a matching rule are dropped from the output unless strict is true, in which case they appear with Rule=nil and Err=nil.

func (Result) OK

func (r Result) OK() bool

OK reports whether the result represents a passing validation.

type Rule

type Rule struct {
	Name     string             // short identifier, e.g. "url"
	Suffixes []string           // uppercase suffixes, e.g. ["_URL", "_URI"]
	Validate func(string) error // required; run against each matched value
	Mask     bool               // true when the value is sensitive
}

Rule classifies a key by one or more case-insensitive suffixes, provides a validation function, and records whether values for matched keys should be masked when displayed.

func MatchRule

func MatchRule(key string) *Rule

MatchRule returns the first rule whose suffix matches key (case-insensitive), or nil when no rule applies.

Jump to

Keyboard shortcuts

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