# Rego Keyword: default

The `default` keyword is used to provide a default value for rules and functions. If in other cases, a rule or function is not defined, the default value will be used.

It is often helpful to have know that a value will _always_ be defined so that policy or callers do not also need to handle undefined values.

## Examples

Denying by default

When default deny behavior is required, knowing that a value will never be undefined is helpful. This is common in access control systems where access is denied unless explicitly allowed.

In the following example, the policy's allow rules depend on fields in `input`. If any field is missing, `allow` should return false instead of undefined. This is achieved using the `default` keyword.

The policy handles unexpected data formats, ensuring the result is always a boolean.

policy.rego

```
package playdefault allow := falseallow if input.admin == trueallow if {	input.path[0] == "users"	input.path[1] == input.user}
```

Output

{
  "allow": false
}

input.json

```
{  "roles": [    "admin"  ],  "path": "/"}
```

data.json

```
{}
```

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

Creating an override function

As shown in the previous example, `default` is helpful for handling undefined values. Handling undefined values is not just important for callers, but also within policies themselves.

The `default` keyword with functions provides a convenient way to set a base case that is overridden when conditions are met.

policy.rego

```
package playdefault max_amount(_, _) := 1000max_amount(overrides, role) := overrides[role]allow if {	input.amount <= max_amount(data.overrides, input.role)}
```

Output

{
  "allow": true
}

input.json

```
{  "amount": 3000,  "role": "staff"}
```

data.json

```
{  "overrides": {    "staff": 10000  }}
```

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