Files
releases/docs/projects/regal/rules/custom/prefer-value-in-head.md
T
Charlie Egan 071f4ea99b docs: Update broken links (#8285)
* Update Regal documentation

Sync documentation with upstream Regal repository to reflect latest changes.

This addresses a number of broken link issues from the checker before.

Signed-off-by: Charlie Egan <charlie_egan@apple.com>

* docs: Update release links

these links create some issues in the link checker report.

https://github.com/open-policy-agent/opa/issues/8278

```
Errors in ./docs/docs/deploy/aws/ec2.mdx

[404] https://github.com/open-policy-agent/opa/releases/download/v%7B%7Bversion%7D%7D/opa_linux_amd64 | Rejected status code (this depends on your "accept" configuration): Not Found
Errors in ./docs/docs/deploy/azure/vm.mdx

[404] https://github.com/open-policy-agent/opa/releases/download/v%7B%7Bversion%7D%7D/opa_linux_amd64 | Error (cached)
Errors in ./docs/docs/deploy/google-cloud/gce.mdx

[404] https://github.com/open-policy-agent/opa/releases/download/v%7B%7Bversion%7D%7D/opa_linux_amd64 | Error (cached)
```

These are not actually broken, they are just untemplated when the checker sees them.

I figured since they are long lines we can use use a $REPO variable instead,
to ensure that we only have valid https:// starting links on those pages.

Signed-off-by: Charlie Egan <charlie_egan@apple.com>

* Remove broken blog link

https://github.com/open-policy-agent/opa/issues/8278

this link appears to be gone with no redirect.

Signed-off-by: Charlie Egan <charlie_egan@apple.com>

* docs: Fix outdated and broken documentation URLs

Update various documentation links, SlideShare links, and external references
that were resulting in redirects.

Fixes https://github.com/open-policy-agent/opa/issues/8278

Signed-off-by: Charlie Egan <charlie_egan@apple.com>

* Update broken regal links

The other rules are using abs links here.

Signed-off-by: Charlie Egan <charlie_egan@apple.com>

---------

Signed-off-by: Charlie Egan <charlie_egan@apple.com>
2026-02-03 16:58:04 +00:00

2.8 KiB

prefer-value-in-head

Summary: Prefer value in rule head

Category: Custom

Avoid

package policy

pin_as_number := val if {
    is_number(input.pin_code)
    val := to_number(input.pin_code)
}

deny contains message if {
    not input.user
    message := "user attribute missing from input"
}

Prefer

package policy

pin_as_number := to_number(input.pin_code) if is_number(input.pin_code)

deny contains "user attribute missing from input" if not input.user

Rationale

Rules that return the value assigned in the last expression of the rule body may have the value, or the function returning the value, moved directly to the rule head. This creates more succinct rules, and often allows for rules to be expressed as "one-liners". This is not a general recommendation, but a style preference that a team or organization might want to standardize on. As such, it is placed in the custom category, and must be explicitly enabled in configuration.

The only-scalars configuration option may be used to only suggest moving scalar values (strings, numbers, booleans, null) to the head, and not expressions or functions returning a value. With this option set to true, the following example would be flagged:

deny contains message if {
    not input.user
    # value is a scalar
    message := "user attribute missing from input"
}

But not:

deny contains message if {
    not input.user
    # value returned from a function call, not suggested if `only-scalars` is set to `true`
    message := sprintf("user attribute missing from input: %v", [input])
}

The include-interpolated configuration option may be used to count interpolated strings as a scalar (string) values, which will have Regal recommend moving them to the head even when only-scalars is set to true.

Configuration Options

This linter rule provides the following configuration options:

rules:
  custom:
    prefer-value-in-head:
      # note that all rules in the "custom" category are disabled by default
      # (i.e. level "ignore")
      #
      # one of "error", "warning", "ignore"
      level: error
      # whether to only suggest moving scalar values (strings, numbers, booleans, null)
      # to the head, and not expressions or functions
      only-scalars: false
      # when set to true, counts interpolated strings as a scalar value, and will suggest
      # moving them to the head even when `only-scalars` is true
      include-interpolated: false
      # variable names to exempt from the rule (by default, none)
      except-var-names:
        - report
        - violation