JuliusBrussee/caveman · error · ErrInvalidRule

: empty name

Error message

%w: empty name

What it means

Every org rule must carry a non-blank Name; compileOrgRules rejects empty or whitespace-only names with ErrInvalidRule. Names identify the rule in the redaction report and in the default [REDACTED:<name>] placeholder, so an unnamed rule is unusable.

Solutions

  1. Give every rule a unique, descriptive Name before calling Payload.
  2. Pre-validate with strings.TrimSpace(r.Name) == "" and drop/fix offending rules upstream.
  3. Treat errors.Is(err, redact.ErrInvalidRule) as a rule-authoring bug and fail fast with the rule index.

Example fix

// before
rules = append(rules, redact.Rule{Type: redact.RuleTypeRegex, Pattern: "sk-[a-z0-9]+"})
// after
rules = append(rules, redact.Rule{Name: "api-key", Type: redact.RuleTypeRegex, Pattern: "sk-[a-z0-9]+"})
Defensive patterns

Strategy: validation

Validate before calling

for i, r := range rules {
    if strings.TrimSpace(r.Name) == "" {
        return fmt.Errorf("rule %d has empty name", i)
    }
}

Try / catch

_, _, err := redact.Payload(body, rules)
if errors.Is(err, redact.ErrInvalidRule) {
    // surface the rule-authoring bug to the operator
}

Prevention

When it happens

Trigger: Appending a Rule{Name: ""} or Name: " " to the rules slice passed to redact.Payload; constructing rules programmatically and forgetting to set Name; decoding rule JSON where the name key is missing.

Common situations: Template-generated rule configs with unfilled name fields; rules built in a loop where the name variable was empty; hand-edited YAML/JSON losing the name key.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/ce82ca3aa25e4bb0. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/redact/payload.go:579

		sb.WriteString(r.Pattern)
		sb.WriteByte('\x1f')
		sb.WriteString(r.Replacement)
		sb.WriteByte('\x1e')

		switch r.Type {
		case RuleTypeBuiltin:
			// A reference to the unconditional floor. Nothing to run, and
			// nothing it could switch off.
			continue
		case RuleTypeRegex:
		case RuleTypeJSONPath, RuleTypeHeader:
			return nil, "", fmt.Errorf("%w: %q (rule %q)", ErrRuleUnsupported, r.Type, r.Name)
		default:
			return nil, "", fmt.Errorf("%w: %q (rule %q)", ErrRuleUnsupported, r.Type, r.Name)
		}

		if strings.TrimSpace(r.Name) == "" {
			return nil, "", fmt.Errorf("%w: empty name", ErrInvalidRule)
		}
		if r.Pattern == "" {
			return nil, "", fmt.Errorf("%w: rule %q has an empty pattern", ErrInvalidRule, r.Name)
		}
		re, err := regexp.Compile(r.Pattern)
		if err != nil {
			return nil, "", fmt.Errorf("%w: rule %q: %s", ErrInvalidRule, r.Name, err)
		}
		if re.MatchString("") {
			// Such a pattern matches at every position and would replace the
			// whole body with placeholders. Refuse it rather than destroy the
			// capture.
			return nil, "", fmt.Errorf("%w: rule %q matches the empty string", ErrInvalidRule, r.Name)
		}
		repl := r.Replacement
		if repl == "" {
			repl = "[REDACTED:" + r.Name + "]"
		}

View on GitHub (pinned to 3ee70a1026)