Bicep doesn't validate the type of variables in a loop
- Dominant language
- Bicep
- Stars
- 3.6k
- Forks
- 830
- Avg merge
- 1d 21m
- Merged PRs (30d)
- 79
Description
**Is your feature request related to a problem? Please describe.**
I am using Bicep v0.21.1 and have enabled "compileTimeImports": true in my bicepconfig.json (though I don't think this is necessary to repro)
I have a Bicep module where I want to pass in an object and use that to create various resources. I defined a custom type for the object and added it as a parameter to the module. In a parent template, I define the object in a variable and pass it to the module. I would like for Bicep to show me where properties are missing in the variable rather than where I reference the variable.
**Example**
`CosmosDbDatabaseWithContainers.module.bicep`
```bicep
@export()
type sqlDatabaseConfigurationType = {
name: string
containerConfigurations: containerConfigurationType[]
}
type containerConfigurationType = {
name: string
indexingPolicy: object
}
param cosmosDbAccountName string
param sqlDatabaseConfiguration sqlDatabaseConfigurationType
resource cosmosDbAccount 'Microsoft.DocumentDB/databaseAccounts@2020-04-01' existing = {
name: cosmosDbAccountName
}
resource sqlDatabase 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2020-03-01' = {
parent: cosmosDbAccount
name: sqlDatabaseConfiguration.name
properties: {
resource: {
id: sqlDatabaseConfiguration.name
}
}
}
@batchSize(1)
resource containers 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2020-03-01' = [for container in sqlDatabaseConfiguration.containerConfigurations: {
parent: sqlDatabase
name: container.name
properties: {
resource: {
id: container.name
indexingPolicy: container.indexingPolicy
}
}
}]
```
`CosmosDbAccount.bicep`
```bicep
import { sqlDatabaseConfigurationType } from 'CosmosDbDatabaseWithContainers.module.bicep'
var defaultIndexingPolicy = { /* omitted for brevity*/ }
var database1Configuration = {
name: 'Database1'
containerConfigurations: [
{
name: 'Collection1'
indexngPolicy: defaultIndexingPolicy // typo in property
}
{
name: 'Collection2' // missing a property
}
]
}
var sqlDatabaseConfigurations = [
database1Configuration
/* other configs omitted */
]
resource cosmosDbAccount 'Microsoft.DocumentDB/databaseAccounts@2020-04-01' = {
name: 'myaccount'
location: 'eastus'
properties: {
databaseAccountOfferType: 'Standard'
locations: [/* omitted */]
}
}
module sqlDatabases 'CosmosDbDatabaseWithContainers.module.bicep' = [for database in sqlDatabaseConfigurations: {
name: database.name
params: {
cosmosDbAccountName: 'myaccount'
sqlDatabaseConfiguration: database1Configuration
}
}]
```
**Describe the solution you'd like**
Problem 1:
The line at the end that reads "sqlDatabaseConfiguration: database1Configuration" shows an error indicating the typo and missing property:

It would be ideal to see these errors show up on the lines where the typo/missing property actually are within the variable, since for a large object that references other variables it could be difficult to spot where the issue is.
Problem 2:
If I change "sqlDatabaseConfiguration: database1Configuration" to "sqlDatabaseConfiguration: database" to use the value from the loop, then Bicep doesn't display any error at all. I would like to see the same error that I see in the above screenshot.

Contributor guide
Research direction
Reproduce the issue using CosmosDbDatabaseWithContainers.module.bicep and CosmosDbAccount.bicep, first checking the diagnostics for database1Configuration and then changing the module argument to the loop variable database. Done means type errors for the misspelled and missing properties point to their locations in the variable, including when the value is passed through the loop.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100