Zum Inhalt springen

Validate a .ccr rule file for syntax and reference errors

Check a single .ccr rule file for syntax errors and unknown property/method references, and print its metadata.

codecharter validate <file> [--license <path>]

<file> is the path to a single .ccr file. The command parses the file's directives and query syntax, then statically resolves every property and method reference in the query against the DSL schema — including through chained access and lambda parameters (e.g. .Where(m => m.DeclaringType.Boddy)) — and prints the rule metadata when both checks succeed. An unknown property or method, such as a typo like t.CompletelyBogusProperty, is now reported here instead of only surfacing later at analyze, test, or dry_run time, with a "did you mean" suggestion when a close match exists.

The one surface this static check intentionally leaves alone is the statement-level Syntax navigation (m.Syntax.Descendants.<NodeKind> and similar): a syntax node's child slots depend on its runtime kind, which validate cannot know statically, so that hop stays dynamically typed and is checked at evaluation time instead. Model-side navigation next to a syntax hop (e.g. m.DeclaringType.Boddy sitting beside m.Syntax...) is still checked.

validate does not check how the rule behaves on real code; use codecharter test to verify a rule against its hit/miss spec cases.

Like every CLI command, validate requires a valid license. Point it at a license file with --license <path> or the CODECHARTER_LICENSE environment variable; see License File.

Example

codecharter validate .codecharter/rules/repository-naming.ccr

Output on success:

Rule 'Repository class must end in Repository' parsed successfully
  Severity: Error
  Category: Naming
  Description: Repositories must have a 'Repository' suffix for discoverability

Omitted directives fall back to defaults in the printed metadata: a missing @name falls back to the file name, a missing @severity defaults to warn (unrecognized severity values are also treated as warn), and a missing @category defaults to General.

On error, a message is written to stderr in the form:

Validation failed: Parse error: ...

An unknown property looks like this:

Validation failed: No member 'LinesOfCod' on Type. Check the predicate catalog for valid properties. Did you mean 'LinesOfCode'?

Exit Codes

  • 0: The file parsed successfully.
  • 2: The file failed to parse or validate; check stderr for Validation failed:.
  • 6: License error (missing, expired, or not valid for this CLI version).

As a pre-commit hook for rule files

#!/bin/sh
for file in $(git diff --cached --name-only --diff-filter=ACMR | grep '\.ccr$'); do
    if codecharter validate "$file" 2>&1 | grep -q 'Validation failed'; then
        echo "Invalid rule file: $file"
        exit 1
    fi
done

The hook checks the output for Validation failed: instead of relying on the exit code. This catches syntax errors in .ccr files before the CI run sees them.