bicep
bicep copied to clipboard
Development-time validation of custom types
I'd like to continue my running history of asking for design-time validation by adding a scenario I was just writing.
I have a custom type for specifying some basic properties for network security groups in which I expect a name and an array of NetworkSecurityGroupRules
to be specified.
@export()
type NsgConfiguration = {
@description('The name of the NSG resource')
name: string
@description('The various NSG rules to create')
rules: NetworkSecurityGroupRule[]
}
I have another longer type I use to define subnets on a virtual network, but I've pasted a smaller subset of it below for brevity in which I expect a name and an optional NsgConfiguration
.
@export()
type Subnet = {
@description('The name of the subnet')
name: string
@description('The NSG configuration for the subnet')
nsgConfiguration: NsgConfiguration?
}
Later on in a module, I have the following that loops through the various defined Subnets (array of Subnet defined in this module) to deploy each of the intended NSGs:
module Nsg 'nsg.bicep' = [for (subnet, index) in Subnets: if (!empty(subnet.nsgConfiguration)){
name: 'testtesttest'
params: {
NsgName: subnet.nsgConfiguration.name
Location: Location
SecurityRules: subnet.nsgConfiguration.rules
}
}]
I include the !empty()
bit because it's possible that the nsgConfiguration
property is null because it's optional on the imported custom type as seen above.
However, when I type this out in VS Code, I get warning squigglies under the name property on the nsgConfiguration:
The warning indicates that "The value of type "
But that's not quite true - per the types I've specified, the name isn't optional and the only way to utilize the values off that type in the parameter is if the parent type is not null as validated by the exists
expression, so ideally, I wouldn't be seeing this message unless the name
were indeed marked as optional.
As always, thank you for the consideration!