domains / cloud-infra / Azure/azure-quickstart-templates
Optional Null Patterns
When a value is optional in Bicep, use **nullable parameter types** and **null-safe property emission** instead of sentinel values (like empty strings) or `json('null')`.
When a value is optional in Bicep, use nullable parameter types and null-safe property emission instead of sentinel values (like empty strings) or json('null').
Apply these rules: 1) Make parameters nullable when they can be omitted
param addressPrefix string?
(or param addressPrefix string = null if your style prefers explicit defaults)
2) For optional object fields, set the field to null (or omit the field)
- Prefer field-level nulling:
properties: { snssai: { sst: sst sd: empty(sd) ? null : sd } } - If the schema requires omitting entirely, conditionally build the object, but keep it consistent:
properties:!empty(sd) ? { snssai: { sst: sst sd: sd } } : { snssai: { sst: sst } }
3) Use native null, not json('null')
zones: (length(availabilityZones) == 0) ? null : availabilityZones
4) Guard optional properties with contains() to avoid “property doesn’t exist” errors
properties: {
nsgId: contains(subnet, 'nsgId') ? (empty(subnet.nsgId) ? null : subnet.nsgId) : null
}
Net effect: templates stay null-safe, avoid invalid payload fields, and reduce runtime/validation errors caused by missing properties or incorrect null representations.