# Rego Keyword Examples: if

The `if` keyword is used when defining rules in Rego. `if` separates the rule head from the rule body, making it clear which part of the rule is the condition (the part following the `if`).

The keyword is also use to make the policy rules written in Rego easier to read by being more 'English-like'. For example:

```
rule := "some value" if some_condition
```

## Examples

Defining simple rules

Most commonly, `if` is used to create boolean rules where if any rule head is true, then the whole rule is true. In this simple example, when both:

*   the `input.role` field is present,
*   and set to the value of "admin"

Then, the `allow` rule will be `true`. If either of these conditions is not met, the rule will be `false`.

policy.rego

```
package playimport rego.v1default allow := falseallow if input.role == "admin"allow if {	input.path[0] == "users"	input.path[1] == input.user_id}
```

Output

{
  "allow": true
}

input.json

```
{  "role": "admin",  "path": [    "payments",    "approve"  ]}
```

data.json

```
{}
```

[Open in OPA Playground](https://play.openpolicyagent.org/?state=eyJpIjoie1xuICBcInJvbGVcIjogXCJhZG1pblwiLFxuICBcInBhdGhcIjogW1xuICAgIFwicGF5bWVudHNcIixcbiAgICBcImFwcHJvdmVcIlxuICBdXG59IiwiZCI6Int9IiwicCI6InBhY2thZ2UgcGxheVxuXG5pbXBvcnQgcmVnby52MVxuXG5kZWZhdWx0IGFsbG93IDo9IGZhbHNlXG5cbmFsbG93IGlmIGlucHV0LnJvbGUgPT0gXCJhZG1pblwiXG5cbmFsbG93IGlmIHtcblx0aW5wdXQucGF0aFswXSA9PSBcInVzZXJzXCJcblx0aW5wdXQucGF0aFsxXSA9PSBpbnB1dC51c2VyX2lkXG59XG4ifQ==)

Defining multi-value rules

The `if` keyword is used for all rules though, including rules that create objects and sets.

policy.rego

```
package playimport rego.v1# missing_paths creates a set missing input pathsmissing_paths contains path if {	some path in data.required_paths	parts := split(path, ".")	object.get(input, parts, "") == ""}# validations is a mapping of input paths to error messagesvalidations[path] contains "path must be set" if {	some path, _ in missing_paths}# role and email have additional validation rulesvalidations.role contains "role cannot be blank" if {	input.role == ""}validations.email contains message if {	not endswith(input.email, "@example.com")	message := sprintf("email %s must end with @example.com", [input.email])}
```

Output

{
  "missing\_paths": \[
    "request.method",
    "role"
  \],
  "validations": {
    "email": \[
      "email admin@example.net must end with @example.com"
    \],
    "request.method": \[
      "path must be set"
    \],
    "role": \[
      "path must be set"
    \]
  }
}

input.json

```
{  "roles": [    "admin"  ],  "email": "admin@example.net",  "request": {    "headers": {},    "path": "/v1/payments"  }}
```

data.json

```
{  "required_paths": [    "request.method",    "request.path",    "request.headers",    "role",    "email"  ]}
```

[Open in OPA Playground](https://play.openpolicyagent.org/?state=eyJpIjoie1xuICBcInJvbGVzXCI6IFtcbiAgICBcImFkbWluXCJcbiAgXSxcbiAgXCJlbWFpbFwiOiBcImFkbWluQGV4YW1wbGUubmV0XCIsXG4gIFwicmVxdWVzdFwiOiB7XG4gICAgXCJoZWFkZXJzXCI6IHt9LFxuICAgIFwicGF0aFwiOiBcIi92MS9wYXltZW50c1wiXG4gIH1cbn0iLCJkIjoie1xuICBcInJlcXVpcmVkX3BhdGhzXCI6IFtcbiAgICBcInJlcXVlc3QubWV0aG9kXCIsXG4gICAgXCJyZXF1ZXN0LnBhdGhcIixcbiAgICBcInJlcXVlc3QuaGVhZGVyc1wiLFxuICAgIFwicm9sZVwiLFxuICAgIFwiZW1haWxcIlxuICBdXG59IiwicCI6InBhY2thZ2UgcGxheVxuXG5pbXBvcnQgcmVnby52MVxuXG4jIG1pc3NpbmdfcGF0aHMgY3JlYXRlcyBhIHNldCBtaXNzaW5nIGlucHV0IHBhdGhzXG5taXNzaW5nX3BhdGhzIGNvbnRhaW5zIHBhdGggaWYge1xuXHRzb21lIHBhdGggaW4gZGF0YS5yZXF1aXJlZF9wYXRoc1xuXG5cdHBhcnRzIDo9IHNwbGl0KHBhdGgsIFwiLlwiKVxuXG5cdG9iamVjdC5nZXQoaW5wdXQsIHBhcnRzLCBcIlwiKSA9PSBcIlwiXG59XG5cbiMgdmFsaWRhdGlvbnMgaXMgYSBtYXBwaW5nIG9mIGlucHV0IHBhdGhzIHRvIGVycm9yIG1lc3NhZ2VzXG52YWxpZGF0aW9uc1twYXRoXSBjb250YWlucyBcInBhdGggbXVzdCBiZSBzZXRcIiBpZiB7XG5cdHNvbWUgcGF0aCwgXyBpbiBtaXNzaW5nX3BhdGhzXG59XG5cbiMgcm9sZSBhbmQgZW1haWwgaGF2ZSBhZGRpdGlvbmFsIHZhbGlkYXRpb24gcnVsZXNcbnZhbGlkYXRpb25zLnJvbGUgY29udGFpbnMgXCJyb2xlIGNhbm5vdCBiZSBibGFua1wiIGlmIHtcblx0aW5wdXQucm9sZSA9PSBcIlwiXG59XG5cbnZhbGlkYXRpb25zLmVtYWlsIGNvbnRhaW5zIG1lc3NhZ2UgaWYge1xuXHRub3QgZW5kc3dpdGgoaW5wdXQuZW1haWwsIFwiQGV4YW1wbGUuY29tXCIpXG5cblx0bWVzc2FnZSA6PSBzcHJpbnRmKFwiZW1haWwgJXMgbXVzdCBlbmQgd2l0aCBAZXhhbXBsZS5jb21cIiwgW2lucHV0LmVtYWlsXSlcbn1cbiJ9)

Defining functions

`if` is also used in functions. Much like rules, in Rego functions can have one or more heads. The head and body of a function are also separated by the `if` keyword for consistency and readability.

In this example, we can see that the `is_sudo` function is incrementally defined where each head adds new cases to the functionality. In this case, each head defines scenarios where the user is a 'sudoer' - both when the user is an admin or when the user has the sudo field set.

policy.rego

```
package playimport rego.v1default is_sudo(_) := falseis_sudo(user) if {	user.role == "admin"}is_sudo(user) if {	user.sudo == true}allow if is_sudo(input.user)
```

Output

{
  "allow": true
}

input.json

```
{  "user": {    "sudo": true  }}
```

data.json

```
{}
```

[Open in OPA Playground](https://play.openpolicyagent.org/?state=eyJpIjoie1xuICBcInVzZXJcIjoge1xuICAgIFwic3Vkb1wiOiB0cnVlXG4gIH1cbn0iLCJkIjoie30iLCJwIjoicGFja2FnZSBwbGF5XG5cbmltcG9ydCByZWdvLnYxXG5cbmRlZmF1bHQgaXNfc3VkbyhfKSA6PSBmYWxzZVxuXG5pc19zdWRvKHVzZXIpIGlmIHtcblx0dXNlci5yb2xlID09IFwiYWRtaW5cIlxufVxuXG5pc19zdWRvKHVzZXIpIGlmIHtcblx0dXNlci5zdWRvID09IHRydWVcbn1cblxuYWxsb3cgaWYgaXNfc3VkbyhpbnB1dC51c2VyKVxuIn0=)

When if is not used

`if` is _everywhere_ in Rego, but there are some cases where we don't use it. In this example, using `if` for a rule name is a parse error.

data.json

```
{}
```

input.json

```
{}
```

policy.rego

```
package playdefault allow := false # not in default casesmy_constant := 42 # not for constants# not for rule namesif {	input.admin}allow if {	# not inside rules	input.admin if input.roles.admin == true}
```

## Further Reading

Below are some links that provide more information about the `if` keyword:

*   If you are interested in learning about why `if` was added to Rego, see the notes in the [OPA v1.0](/docs/v0-upgrade) documentation.
*   Read the release notes from when the `if` keyword was added to Rego in [OPA v0.42.0](https://github.com/open-policy-agent/opa/releases/tag/v0.42.0).
*   Using `if` is also [recommended by Regal](/projects/regal/rules/idiomatic/use-if).