metric rule type enables you to create custom formulas based on document statistics. It’s like the readability rule, but with complete control over the formula and condition.
How It Works
Themetric rule extracts document statistics (word count, sentence count, heading counts, etc.), evaluates your custom formula, and checks if the result meets your specified condition.
Parameters
A mathematical expression using document statistics as variables. Supports arithmetic operations and the
math module.A boolean condition to evaluate the formula result against (e.g.,
"> 10", "< 5", ">= 8").Available Variables
Vale provides these document statistics as variables:| Variable | Description |
|---|---|
words | Total word count |
sentences | Total sentence count |
characters | Total character count |
paragraphs | Total paragraph count |
pre | Number of code blocks |
list | Number of list items |
blockquote | Number of blockquotes |
heading_h1 | Number of H1 headings |
heading_h2 | Number of H2 headings |
heading_h3 | Number of H3 headings |
heading_h4 | Number of H4 headings |
heading_h5 | Number of H5 headings |
heading_h6 | Number of H6 headings |
You can also use
heading.h1 through heading.h6 syntax, which Vale automatically converts to heading_h1 format.Examples
Words per Sentence
Enforce average sentence length:Automated Readability Index
Implement a custom readability formula:Heading Density
Ensure sufficient heading structure:Paragraph Length Limit
Limit average paragraph length:Code-to-Text Ratio
Ensure balance between code and explanation:Complex Condition
Use multiple conditions:List Item Density
Ensure sufficient explanatory text:Minimum Content
Require minimum content:Mathematical Operations
The formula supports standard arithmetic:- Addition:
+ - Subtraction:
- - Multiplication:
* - Division:
/ - Parentheses:
() - Math module functions:
math.sqrt(),math.floor(),math.ceil(), etc.
Using Math Module
Condition Operators
Valid comparison operators:- Greater than:
">" - Less than:
"<" - Greater or equal:
">=" - Less or equal:
"<=" - Equal:
"==" - Not equal:
"!="
Use Cases
The
metric rule is ideal for:- Custom readability formulas
- Document structure validation
- Content density requirements
- Style guide compliance metrics
- Quality control thresholds
- Balanced content ratios
Multi-line Formulas
Use YAML’s pipe (|) for readable multi-line formulas:
Technical Details
Internally, themetric rule (internal/check/metric.go:50-96):
- Calls
f.ComputeMetrics()to extract document statistics - Evaluates the formula using the Tengo scripting engine
- Evaluates the condition as a boolean expression
- If the condition is true, creates an alert at line 1
Message Formatting
The%s placeholder is replaced with the calculated result:
Scope Behavior
Zero Division Handling
If variables could be zero (empty document sections), handle division carefully:Advanced Example: Content Quality Score
Combine multiple factors:Debugging Formulas
To debug your formula:- Start simple:
formula: words - Add operations one at a time
- Check error messages for syntax issues
- Verify variable names (check spelling)
- Test with documents of known statistics
Performance
Metric calculations are fast because:- Statistics are computed once per file
- Formulas are compiled, not interpreted
- No regex or text processing during evaluation
Related Rule Types
- readability: Use for standard readability formulas
- occurrence: Use for simple token counting without formulas
- script: Use for more complex logic beyond mathematical formulas