Custom Codeac Rules: Tailoring Static Analysis with YAML
Learn how to extend Codeac’s static analysis by writing lightweight YAML rules that enforce project‑specific coding standards. From authoring to CI integration, this guide walks you through a concrete example, trade‑offs, and best practices.
06 May 2026, 01:50 UTC

Problem: One‑Size‑Doesn’t‑Fit‑All in Static Analysis
When a team adopts a static analysis tool, the built‑in rules often cover general best practices but miss project‑specific conventions. For example, a legacy codebase might mandate that all service classes use a particular naming pattern or that business logic never exceeds a certain cyclomatic complexity. Relying solely on default Codeac checks means either ignoring these nuances or manually reviewing every commit – both undesirable.
Thesis: Use YAML‑Based Custom Rules to Encode Your Own Standards
Codeac lets you write lightweight rules in YAML, which are validated against a schema before deployment. This approach keeps rule definitions human‑readable, version‑controlled, and easily testable locally and in CI.
1. Rule Authoring Basics
YAML is indentation‑sensitive but concise. A rule file typically contains:
id– unique identifier.description– human‑readable explanation.severity– one ofinfo,warning,error.language– target language (e.g.,java,python).files– glob patterns to include or exclude.rule– the actual matcher, often a regex or a simple expression.
Codeac’s schema validator will catch indentation mistakes, missing keys, or unsupported values before the rule is accepted.
Example: Flag Methods Exceeding 30 Lines
Below is a minimal rule that triggers an error when any method in a Java file contains more than 30 lines. The rule uses a simple line‑count matcher provided by Codeac.
id: long-method
description: "Methods should not exceed 30 lines to keep logic readable"
severity: warning
language: java
files:
- "src/**/*.java"
rule:
type: line_count
max_lines: 30
Save this as long-method.yml in your repository’s .codeac/rules directory.
2. Local Validation and Testing
- Run Codeac locally on a test branch:
codeac run --config .codeac/config.yml - Inspect the generated issue list. Each violation should reference
long-methodand point to the offending method. - Adjust
max_linesto 20 and rerun. Observe an increase in reported issues, confirming sensitivity. - Use a YAML linter (e.g.,
yamllint) before committing to catch indentation errors that could silently fail during validation.
Codeac’s UI will highlight schema errors in the rule file if you upload it through the web interface.
3. CI Integration
To enforce the rule automatically, add a Codeac step to your CI pipeline. For a GitHub Actions workflow, the snippet might look like:
name: Codeac Static Analysis
on: [pull_request]
jobs:
analyze:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Codeac
run: curl -fsSL https://codeac.io/install.sh | sh
- name: Run Codeac
run: codeac run --config .codeac/config.yml
- name: Upload results
uses: actions/upload-artifact@v4
with:
name: codeac-results
path: codeac-report.json
When a PR is opened or updated, Codeac will run, and any violations will surface in the PR review, preventing merge until addressed.
4. Trade‑offs and Limitations
- False Positives – A poorly scoped regex or overly aggressive threshold can flag legitimate code. Test on a representative codebase before merging to main.
- Performance – Complex regexes or large file patterns can slow analysis. Keep patterns simple and limit file scopes when possible.
- Balance with Built‑in Rules – Relying exclusively on custom rules can inadvertently bypass Codeac’s default best‑practice checks. Keep a mix to maintain overall quality.
- YAML Linting – Indentation errors may be silent. Run
yamllintas part of the CI job to catch these early.
Actionable Closing: Quick Checklist
- Create the rule YAML in
.codeac/rules. - Run
yamllintlocally. - Execute
codeac runto validate against a sample repo. - Commit the rule and push to a feature branch.
- Open a PR and verify that Codeac runs in CI, producing the expected warnings.
- Iterate: adjust thresholds or patterns until the rule aligns with your team’s expectations.
By following these steps, you embed project‑specific standards into the development workflow, ensuring consistent code quality without sacrificing flexibility.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.