Sdílet prostřednictvím


Pravidlo Linteru – zjednodušení hodnoty NULL JSON

Toto pravidlo najde json('null').

Kód pravidla Linteru

K přizpůsobení nastavení pravidel použijte v konfiguračním souboru Bicep následující hodnotu:

simplify-json-null

Řešení

Následující příklad selže s tímto testem, protože json('null') se používá:

@description('The name of the API Management service instance')
param apiManagementServiceName string = 'apiservice${uniqueString(resourceGroup().id)}'

@description('The email address of the owner of the service')
@minLength(1)
param publisherEmail string

@description('The name of the owner of the service')
@minLength(1)
param publisherName string

@description('The pricing tier of this API Management service')
@allowed([
  'Premium'
])
param sku string = 'Premium'

@description('The instance size of this API Management service.')
param skuCount int = 3

@description('Location for all resources.')
param location string = resourceGroup().location

@description('Zone numbers e.g. 1,2,3.')
param availabilityZones array = [
  '1'
  '2'
  '3'
]

resource apiManagementService 'Microsoft.ApiManagement/service@2022-08-01' = {
  name: apiManagementServiceName
  location: location
  zones: ((length(availabilityZones) == 0) ? json('null') : availabilityZones)
  sku: {
    name: sku
    capacity: skuCount
  }
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    publisherEmail: publisherEmail
    publisherName: publisherName
  }
}

Syntaxi můžete zjednodušit nahrazením json('null')null:

@description('The name of the API Management service instance')
param apiManagementServiceName string = 'apiservice${uniqueString(resourceGroup().id)}'

@description('The email address of the owner of the service')
@minLength(1)
param publisherEmail string

@description('The name of the owner of the service')
@minLength(1)
param publisherName string

@description('The pricing tier of this API Management service')
@allowed([
  'Premium'
])
param sku string = 'Premium'

@description('The instance size of this API Management service.')
param skuCount int = 3

@description('Location for all resources.')
param location string = resourceGroup().location

@description('Zone numbers e.g. 1,2,3.')
param availabilityZones array = [
  '1'
  '2'
  '3'
]

resource apiManagementService 'Microsoft.ApiManagement/service@2022-08-01' = {
  name: apiManagementServiceName
  location: location
  zones: ((length(availabilityZones) == 0) ? null : availabilityZones)
  sku: {
    name: sku
    capacity: skuCount
  }
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    publisherEmail: publisherEmail
    publisherName: publisherName
  }
}

Syntaxi můžete zjednodušit výběrem rychlé opravy , jak je znázorněno na následujícím snímku obrazovky:

Snímek obrazovky s rychlou opravou pro zjednodušení kódu JSON s hodnotou null

Další kroky

Další informace o linteru naleznete v tématu Použití linter Bicep.