JuliusBrussee/caveman · error · ErrInvalidRule

: rule has an empty pattern

Error message

%w: rule %q has an empty pattern

What it means

A regex rule with an empty Pattern is rejected with ErrInvalidRule because there is nothing to match. The pattern is required for RuleTypeRegex rules; the rule name is included in the message for easy location.

Solutions

  1. Supply a valid regex in Pattern, or remove the rule entirely instead of leaving it empty.
  2. Pre-validate r.Pattern != "" for regex rules before calling Payload.
  3. If a rule should be temporarily disabled, filter it out of the slice rather than blanking its pattern.

Example fix

// before
rules := []redact.Rule{{Name: "cards", Type: redact.RuleTypeRegex, Pattern: ""}}
// after
rules := []redact.Rule{{Name: "cards", Type: redact.RuleTypeRegex, Pattern: "\\b(?:\\d[ -]*?){13,16}\\b"}}
Defensive patterns

Strategy: validation

Validate before calling

for _, r := range rules {
    if r.Type == redact.RuleTypeRegex && r.Pattern == "" {
        return fmt.Errorf("rule %q has empty pattern", r.Name)
    }
}

Try / catch

_, _, err := redact.Payload(body, rules)
if errors.Is(err, redact.ErrInvalidRule) {
    // fix or drop the empty rule before retrying
}

Prevention

When it happens

Trigger: Passing a Rule with Type RuleTypeRegex and Pattern "" to redact.Payload; rules decoded from config where the pattern key was omitted or set to an empty string.

Common situations: Rule authoring UIs/exporters emitting placeholder entries; deleting a pattern from a rule file intending to disable the rule instead of removing it entirely.

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/7fe64f7d42afb1e1. Report an issue: GitHub.

Appendix: source

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

		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 + "]"
		}
		out = append(out, compiledRule{
			name:                 r.Name,
			origin:               OriginOrg,

View on GitHub (pinned to 3ee70a1026)