Bicep诊断代码 - BCP364

@discriminator() 使用装饰器,但指定的判别器属性并非每个工会成员类型都必须的字符串字面值时,就会发生这种诊断。 判别子属性必须是必需的(非可选),并且所有联合成员必须有字符串字面类型(而非通用 string 类型)。

Description

属性“<discriminator-property-name>”必须是所有工会成员类型中必填字符串的字面值。

级别

Error

解决方案

确保区分性属性:

  • 所有工会会员类型均存在且必须(无 ? 后缀)。
  • 是字符串字面量(例如, 'foo'),而非一般 string 类型。

Examples

以下示例引发了诊断问题,因为该type属性完全缺失于:FooConfig

type FooConfig = {
  value: int
}

type BarConfig = {
  type: 'bar'
  value: bool
}

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

你可以通过为所有 union 成员类型添加所需的 type 字符串文字属性来修复诊断问题:

type FooConfig = {
  type: 'foo'
  value: int
}

type BarConfig = {
  type: 'bar'
  value: bool
}

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

param config ServiceConfig

以下示例提出了诊断问题,因为该type属性在上?是可选的(用 标记为 FooConfig):

type FooConfig = {
  type?: 'foo'
  value: int
}

type BarConfig = {
  type: 'bar'
  value: bool
}

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

你可以通过设置 type 所有工会成员类型所需的属性来修正诊断:

type FooConfig = {
  type: 'foo'
  value: int
}

type BarConfig = {
  type: 'bar'
  value: bool
}

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

param config ServiceConfig

以下示例提出了诊断问题,因为 type 上的 FooConfig 属性类型为一般 string 类型而非字符串字面:

type FooConfig = {
  type: string
  value: int
}

type BarConfig = {
  type: 'bar'
  value: bool
}

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

你可以通过将 type 所有联合成员类型改为字符串字面量来修复诊断问题:

type FooConfig = {
  type: 'foo'
  value: int
}

type BarConfig = {
  type: 'bar'
  value: bool
}

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

param config ServiceConfig

以下示例提出了诊断问题,因为 type 上的 FooConfig 属性是整数文字而非字符串文字:

type FooConfig = {
  type: 1
  value: int
}

type BarConfig = {
  type: 'bar'
  value: bool
}

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

你可以通过将 type 所有联合成员类型改为字符串字面量来修复诊断问题:

type FooConfig = {
  type: 'foo'
  value: int
}

type BarConfig = {
  type: 'bar'
  value: bool
}

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

param config ServiceConfig

以下示例提出了诊断性,因为 typeFooConfig 属性是字符串文字的并集,而非单一字符串文字:

type FooConfig = {
  type: 'foo' | 'baz'
  value: int
}

type BarConfig = {
  type: 'bar'
  value: bool
}

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

你可以通过为每个联合成员类型赋予属性一个字符串字面值 type 来修复诊断:

type FooConfig = {
  type: 'foo'
  value: int
}

type BarConfig = {
  type: 'bar'
  value: bool
}

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

param config ServiceConfig

后续步骤

有关 Bicep 诊断的详细信息,请参阅 Bicep 核心诊断