reference Commands

opengrep validate

See also
opengrep test, opengrep scan, --config, --skip-invalid-configs, --strict, --validate, rules
opengrep validate [FLAGS] PATHS...

Reads the rules under PATHS and reports whether they are valid. PATHS are rule files, or directories that contain them, given as arguments: unlike scan and test, validate has no --config. Without a path, it stops with Nothing to validate, pass a directory or rule file and exit status 2.

Each rule is checked in three ways:

The metachecks run on rule files, so they apply only to rules from a local file or directory. Rules from the registry, a URL or a git repository are parsed but not checked by the metachecks, and validate logs a warning when no local rule file remains for them to check.

All the output of validate, the result included, goes to standard error. Before the result, it reports the scan of the metachecks: the number of rule files and of metachecks, such as 1 file · 6 rules.

Exit status

StatusMeaning
0Every rule is valid.
2A fatal error, such as a missing path.
3A rule file could not be parsed as a target of the metachecks.
4A rule does not conform to the schema, or a pattern could not be parsed.
5A rule file is not valid YAML.
7No rules could be loaded at all.
8A rule names an unsupported language.
141The program that reads the output closed the pipe.

Flags

FlagSummary
--debugLog the messages of --verbose and the diagnostics of the engine.
--developListed in Internal and debugging interfaces.
--force-colorStyle the output even when it does not go to a terminal.
--profileListed in Internal and debugging interfaces.
--quietPrint only the findings, and nothing on standard error.
--skinThe layout of the text output.
--verboseLog the progress of the run, at the info level.

Examples

A valid rule file

good.yaml
rules:  - id: find-eval    pattern: eval(...)    message: found eval    languages: [python]    severity: WARNING
Command and result
$ opengrep validate good.yaml 2>&1 >/dev/null
1 file · 6 rules

Configuration is valid - found 0 fatal errors, 0 skippable error(s), and 1 rule(s).
$ opengrep validate good.yaml > /dev/null 2>&1; echo "exit status: $?"
exit status: 0

A rule that does not conform to the schema

bad.yaml
rules:  - id: broken    patterns:      - pattern-not: eval("...")    message: no positive term    languages: [python]    severity: WARNING
Command and result
$ opengrep validate bad.yaml 2>&1 >/dev/null
warning  bad.yaml:3:5: Invalid rule schema in rule broken
           --> bad.yaml:3
         2 |   - id: broken
         3 |     patterns:
           |     ^^^^^^^^
         4 |       - pattern-not: eval("...")
         you need at least one positive term (not just negations or conditions)
1 file · 6 rules

Configuration is invalid - found 0 fatal errors, 1 skippable error(s), and 0 rule(s).
error    Please fix the above errors and try again.
$ opengrep validate bad.yaml > /dev/null 2>&1; echo "exit status: $?"
exit status: 4