You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-resource-manager/bicep/data-types.md
+32-3Lines changed: 32 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -319,12 +319,14 @@ is ${blocked}'''
319
319
320
320
## Union types
321
321
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.
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
335
337
type baz = bar | (4 | 5) | 6
336
338
```
337
339
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.
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
+
338
367
There are some limitations with union type.
339
368
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:
341
370
342
371
```bicep
343
372
type foo = 'a' | 1
344
373
```
345
374
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).
Copy file name to clipboardExpand all lines: articles/azure-resource-manager/bicep/parameters.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -105,7 +105,7 @@ The following table describes the available decorators and how to use them.
105
105
106
106
| Decorator | Apply to | Argument | Description |
107
107
| --------- | ---- | ----------- | ------- |
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.|
109
109
|[description](#description)| all | string | Text that explains how to use the parameter. The description is displayed to users through the portal. |
110
110
|[maxLength](#length-constraints)| array, string | int | The maximum length for string and array parameters. The value is inclusive. |
111
111
|[maxValue](#integer-constraints)| int | int | The maximum value for the integer parameter. This value is inclusive. |
Copy file name to clipboardExpand all lines: articles/azure-resource-manager/bicep/user-defined-data-types.md
+9-15Lines changed: 9 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,16 +12,15 @@ Learn how to use user-defined data types in Bicep. For system-defined data types
12
12
13
13
[Bicep CLI version 0.12.X or higher](./install.md) is required to use this feature.
14
14
15
-
## User-defined data type syntax
15
+
## The syntax
16
16
17
17
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.
18
18
19
19
```bicep
20
20
type <user-defined-data-type-name> = <type-expression>
21
21
```
22
22
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).
25
24
26
25
The valid type expressions include:
27
26
@@ -95,13 +94,16 @@ The valid type expressions include:
95
94
}
96
95
```
97
96
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:
99
98
100
99
```bicep
100
+
type directions = 'east' | 'south' | 'west' | 'north'
101
+
101
102
type obj = {
102
103
level: 'bronze' | 'silver' | 'gold'
103
104
}
104
105
```
106
+
105
107
**Recursion**
106
108
107
109
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:
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
237
231
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:
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).
0 commit comments