|
| 1 | +# Azure Bicep - Validate Decorator |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +The `@validate()` decorator in Azure Bicep allows you to add custom validation logic to your parameters. This enables you to enforce complex constraints that go beyond basic type checking, catching configuration errors at deployment time with clear error messages. |
| 6 | + |
| 7 | +This feature was released in [Bicep v0.32](https://github.com/Azure/bicep/releases/tag/v0.32.4) as an experimental feature. |
| 8 | + |
| 9 | +## 📃 Benefits of the Validate Decorator |
| 10 | + |
| 11 | +✅ **Custom Validation**: Define complex validation rules using lambda expressions that return true or false. |
| 12 | + |
| 13 | +✅ **Clear Error Messages**: Validation errors include your custom message, making it easy to understand what went wrong. |
| 14 | + |
| 15 | +✅ **ARM-Level Enforcement**: Validation is enforced by Azure Resource Manager at deployment time, ensuring invalid configurations are rejected before resources are created. |
| 16 | + |
| 17 | +## ⚗️ Enabling the Experimental Feature |
| 18 | + |
| 19 | +The `@validate()` decorator is currently an experimental feature. To enable it, add the following to your `bicepconfig.json` file: |
| 20 | + |
| 21 | +```json |
| 22 | +{ |
| 23 | + "experimentalFeaturesEnabled": { |
| 24 | + "userDefinedConstraints": true |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +## Example |
| 30 | + |
| 31 | +In this example, the `@validate()` decorator uses a lambda expression to enforce that a CORS allowed origin FQDN does not contain protocol prefixes and is a valid domain format. The template then prepends `https://` to the validated FQDN when configuring the Storage Account CORS rules. |
| 32 | + |
| 33 | +```bicep |
| 34 | +@description('Allowed origin FQDN for CORS. Must not contain https:// or http:// prefix.') |
| 35 | +@validate( |
| 36 | + x => !contains(x, 'https://') && !contains(x, 'http://') && contains(x, '.'), |
| 37 | + 'The allowed origin FQDN must not contain "https://" or "http://" prefix and must be a valid domain name.' |
| 38 | +) |
| 39 | +param allowedOriginFqdn string |
| 40 | +``` |
| 41 | + |
| 42 | +The lambda syntax `x => expression` allows you to reference the parameter value within the validation expression. You can combine multiple conditions using logical operators like `&&` (and) and `||` (or). |
| 43 | + |
| 44 | +> [!IMPORTANT] |
| 45 | +> The `@validate()` decorator is enforced at **ARM deployment time**, not at Bicep compile time. This means you need to deploy the template to see validation errors. If you want alternative options ahead of ARM engine deployment, look at the Bicep Test Framework and the `fail()` function. |
| 46 | +
|
| 47 | +### Validation Error Example |
| 48 | + |
| 49 | +If you were to deploy with an invalid value like `https://app.contoso.com`, ARM would reject the deployment with an error similar to: |
| 50 | + |
| 51 | +```plaintext |
| 52 | +{"code": "InvalidTemplate", "message": "Deployment template validation failed: The template parameter 'allowedOriginFqdn' failed validation. The allowed origin FQDN must not contain \"https://\" or \"http://\" prefix and must be a valid domain name."} |
| 53 | +``` |
| 54 | + |
| 55 | +This enables authors to define meaningful validation messages that surface during deployment, providing clear guidance on how to resolve the issue. |
| 56 | + |
| 57 | +## 🚀 Deployment |
| 58 | + |
| 59 | +> [!NOTE] |
| 60 | +> You need to have a resource group deployed before trying this out. |
| 61 | +
|
| 62 | +In Visual Studio Code open a terminal and run: |
| 63 | + |
| 64 | +CLI |
| 65 | + |
| 66 | +```bash |
| 67 | +az login |
| 68 | +az account set --subscription 'subscription name or id' |
| 69 | +az deployment group create -g 'your-rg' --confirm-with-what-if -f './main.bicep' -p 'main.bicepparam' |
| 70 | +``` |
| 71 | + |
| 72 | +or PowerShell |
| 73 | + |
| 74 | +```powershell |
| 75 | +Connect-AzAccount |
| 76 | +Set-AzContext -Subscription "subscription name or id" |
| 77 | +New-AzResourceGroupDeployment -Confirm -ResourceGroup "your-rg" -TemplateFile "main.bicep" -TemplateParameterFile "main.bicepparam" |
| 78 | +``` |
0 commit comments