Skip to content

codecharter validate

Check a single .ccr rule file for syntax errors 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 and prints the rule metadata when parsing succeeds. It does not check whether the properties used in the query exist or 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: ...

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.