conditional rule type enforces logical dependencies between patterns. It ensures that if a “first” pattern appears, a corresponding “second” pattern must also exist in the document.
How It Works
Theconditional rule implements an antecedent-consequent relationship:
- It searches for the “second” pattern (the consequent/definition)
- It stores all matches in a file-level registry
- It then searches for the “first” pattern (the antecedent)
- If “first” is found but wasn’t registered in step 2, it triggers an alert
Parameters
A regex pattern representing the antecedent—the pattern that triggers the requirement for “second”. This typically includes a capture group.
A regex pattern representing the consequent—the pattern that must exist when “first” is found. This typically includes a capture group that matches the captured value from “first”.
Makes pattern matching case-insensitive when set to
true.An array of strings or patterns that should be excluded from the “first” check.
If
true, Vale will use your vocabulary files as additional exceptions.Examples
Acronym Definitions
Ensure acronyms are defined before use:- ❌ “Use WHO guidelines” (no definition)
- ✅ “Use World Health Organization (WHO) guidelines” (defined)
- ✅ “WHO: World Health Organization” (defined)
Technical Term Introduction
Require technical terms to be introduced with their full name:Variable Declaration
Ensure variables are declared before use (in code examples):Cross-Reference Validation
Ensure referenced sections exist:Brand Name Introduction
Require brand names to be introduced with registration marks:Use Cases
The
conditional rule is ideal for:- Enforcing acronym definitions
- Validating cross-references
- Checking prerequisite explanations
- Ensuring proper term introductions
- Validating code examples (variable declarations)
Capture Groups
Both
first and second patterns should include capture groups. The captured values from second are stored in the file registry, and the captured value from first is checked against this registry.The typical pattern is:first: Captures the term to validatesecond: Captures the same term in its definition/introduction context
Order of Evaluation
Technical Details
Internally, theconditional rule (internal/check/conditional.go:60-106):
- Searches for all matches of the
secondpattern (consequent) - Extracts capture groups and stores them in
f.Sequences - Searches for all matches of the
firstpattern (antecedent) - For each
firstmatch, checks if it exists inf.Sequences - If not found and not in exceptions, creates an alert
Multiple Capture Groups
Both patterns can use multiple capture groups. All captured values fromsecond are registered:
Related Rule Types
- existence: Use when you want to flag terms without requiring definitions
- sequence: Use when you need to enforce order or proximity of patterns
- consistency: Use when you want to ensure variants aren’t mixed