Skip to content

Commit f16c696

Browse files
committed
Consolidate the information on union type
1 parent 22fd29a commit f16c696

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

articles/azure-resource-manager/bicep/data-types.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,14 @@ is ${blocked}'''
319319

320320
## Union types
321321

322-
In Bicep, a union type allows the creation of a combined type consisting of a set of sub-types. An assignment is valid if any of the individual sub-type assignments are permitted. The `|` character separates individual sub-types using an `or` condition. For example, the syntax `'a' | 'b'` means that a valid assignment could be either `'a'` or `'b'`.
322+
In Bicep, a union type allows the creation of a combined type consisting of a set of sub-types. An assignment is valid if any of the individual sub-type assignments are permitted. The `|` character separates individual sub-types using an `or` condition. For example, the syntax `'a' | 'b'` means that a valid assignment could be either `'a'` or `'b'`. Union types are translated into the allowed-value constraint in Bicep, so only literals are permitted as members. Unions may include any number of literal-typed expressions.
323323

324324
```bicep
325325
type color = 'Red' | 'Blue' | 'White'
326326
type trueOrFalse = 'true' | 'false'
327327
type permittedIntegers = 1 | 2 | 3
328+
type oneOfSeveralObjects = {foo: 'bar'} | {fizz: 'buzz'} | {snap: 'crackle'}
329+
type mixedTypeArray = ('fizz' | 42 | {an: 'object'} | null)[]
328330
```
329331

330332
Any type expression can be used as a sub-type in a union type declaration (between `|` characters). For example, the following examples are all valid:
@@ -335,15 +337,42 @@ type bar = foo | 3
335337
type baz = bar | (4 | 5) | 6
336338
```
337339

340+
### Custom-tagged union data type
341+
342+
Bicep supports custom tagged union data type, which is used to represent a value that can be one of several different types. To declare a custom tagged union data type, you can use a `@discriminator()` decorator. [Bicep CLI version 0.21.X or higher](./install.md) is required to use this decorator. The syntax is:
343+
344+
```bicep
345+
@discriminator('<property-name>')
346+
```
347+
348+
The discriminator decorator takes a single parameter, which represents a shared property name among all union members. This property name must be a required string literal on all members and is case-sensitive. The values of the discriminated property on the union members must be unique in a case-insensitive manner.
349+
350+
```bicep
351+
type FooConfig = {
352+
type: 'foo'
353+
value: int
354+
}
355+
356+
type BarConfig = {
357+
type: 'bar'
358+
value: bool
359+
}
360+
361+
@discriminator('type')
362+
param ServiceConfig FooConfig | BarConfig | { type: 'baz', *: string } = { type: 'bar', value: true }
363+
```
364+
365+
The parameter value is validated based on the discriminated property value. For instance, in the preceding example, if the _serviceConfig_ parameter is of type _foo_, it is validated using the _FooConfig_ type. Similarly, if the parameter is of type _bar_, it is validated using the _BarConfig_ type. This pattern applies to other types as well.
366+
338367
There are some limitations with union type.
339368

340-
* Union types must be reduceable to a single ARM type. The following definition is in valid:
369+
* Union types must be reduceable to a single ARM type. The following definition is invalid:
341370

342371
```bicep
343372
type foo = 'a' | 1
344373
```
345374

346-
The union type syntax can also be used in [user-defined data types](./user-defined-data-types.md).
375+
The union type syntax can be used in [user-defined data types](./user-defined-data-types.md).
347376

348377
## Secure strings and objects
349378

articles/azure-resource-manager/bicep/parameters.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ The following table describes the available decorators and how to use them.
105105

106106
| Decorator | Apply to | Argument | Description |
107107
| --------- | ---- | ----------- | ------- |
108-
| [allowed](#allowed-values) | all | array | Allowed values for the parameter. Use this decorator to make sure the user provides correct values. |
108+
| [allowed](#allowed-values) | all | array | Use this decorator to make sure the user provides correct values. This decorator is only permitted on `param` statements. To declare that a property must be one of a set of predefined values in a [`type`](./user-defined-data-types.md) or [`output`](./outputs.md) statement, use [union type syntax](./data-types.md#union-types). Union type syntax may also be used in `param` statements.|
109109
| [description](#description) | all | string | Text that explains how to use the parameter. The description is displayed to users through the portal. |
110110
| [maxLength](#length-constraints) | array, string | int | The maximum length for string and array parameters. The value is inclusive. |
111111
| [maxValue](#integer-constraints) | int | int | The maximum value for the integer parameter. This value is inclusive. |

articles/azure-resource-manager/bicep/user-defined-data-types.md

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@ Learn how to use user-defined data types in Bicep. For system-defined data types
1212

1313
[Bicep CLI version 0.12.X or higher](./install.md) is required to use this feature.
1414

15-
## User-defined data type syntax
15+
## The syntax
1616

1717
You can use the `type` statement to define user-defined data types. In addition, you can also use type expressions in some places to define custom types.
1818

1919
```bicep
2020
type <user-defined-data-type-name> = <type-expression>
2121
```
2222

23-
> [!NOTE]
24-
> The [`@allowed` decorator](./parameters.md#decorators) is only permitted on [`param` statements](./parameters.md). To declare that a property must be one of a set of predefined values in a `type` or [`output`](./outputs.md) statement, use union type syntax. Union type syntax may also be used in [`param` statements](./parameters.md).
23+
The [`@allowed`](./parameters.md#decorators) decorator is only permitted on [`param` statements](./parameters.md). To declare that a property with a set of predefined values in a `type`, use [union type syntax](./data-types.md#union-types).
2524

2625
The valid type expressions include:
2726

@@ -95,13 +94,16 @@ The valid type expressions include:
9594
}
9695
```
9796
98-
The following sample shows how to use the union type syntax to list a set of predefined values:
97+
The following sample shows how to use the [union type syntax](./data-types.md#union-types) to list a set of predefined values:
9998
10099
```bicep
100+
type directions = 'east' | 'south' | 'west' | 'north'
101+
101102
type obj = {
102103
level: 'bronze' | 'silver' | 'gold'
103104
}
104105
```
106+
105107
**Recursion**
106108
107109
Object types may use direct or indirect recursion so long as at least leg of the path to the recursion point is optional. For example, the `myObjectType` definition in the following example is valid because the directly recursive `recursiveProp` property is optional:
@@ -225,17 +227,9 @@ resource storageAccount 'Microsoft.Storage/storageAccounts@2023-04-01' = {
225227
}
226228
```
227229

228-
## Declare tagged union type
229-
230-
To declare a custom tagged union data type within a Bicep file, you can place a discriminator decorator above a user-defined type declaration. [Bicep CLI version 0.21.X or higher](./install.md) is required to use this decorator. The syntax is:
231-
232-
```bicep
233-
@discriminator('<propertyName>')
234-
```
235-
236-
The discriminator decorator takes a single parameter, which represents a shared property name among all union members. This property name must be a required string literal on all members and is case-sensitive. The values of the discriminated property on the union members must be unique in a case-insensitive manner.
230+
## Tagged union data type
237231

238-
The following example shows how to declare a tagged union type:
232+
To declare a custom tagged union data type within a Bicep file, you can place a discriminator decorator above a user-defined type declaration. [Bicep CLI version 0.21.X or higher](./install.md) is required to use this decorator. The following example shows how to declare a tagged union data type:
239233

240234
```bicep
241235
type FooConfig = {
@@ -256,7 +250,7 @@ param serviceConfig ServiceConfig = { type: 'bar', value: true }
256250
output config object = serviceConfig
257251
```
258252

259-
The parameter value is validated based on the discriminated property value. In the preceding example, if the *serviceConfig* parameter value is of type *foo*, it undergoes validation using the *FooConfig*type. Likewise, if the parameter value is of type *bar*, validation is performed using the *BarConfig* type, and this pattern continues for other types as well.
253+
For more information, see [Custom tagged union data type](./data-types.md#custom-tagged-union-data-type).
260254

261255
## Import types between Bicep files
262256

0 commit comments

Comments
 (0)