Back to Opa

one-liner-rule

docs/projects/regal/rules/custom/one-liner-rule.md

1.16.11.2 KB
Original Source

one-liner-rule

Summary: Rule body could be made a one-liner

Category: Custom

Avoid

rego
package policy

allow if {
    is_admin
}

is_admin if {
    "admin" in input.user.roles
}

Prefer

rego
package policy

allow if is_admin

is_admin if "admin" in input.user.roles

Rationale

Rules with only a single expression in the body may omit the curly braces around the body, and be written as a one-liner. This makes simple rules read more like English, and will have more rules fit on the screen.

As with other rules in the custom category, this is not necessarily a general recommendation, but a style preference teams or organizations might want to standardize on. As such, it must be enabled via configuration.

Configuration Options

This linter rule provides the following configuration options:

yaml
rules:
  custom:
    one-liner-rule:
      # note that all rules in the "custom" category are disabled by default
      # (i.e. level "ignore")
      #
      # one of "error", "warning", "ignore"
      level: error