bicep icon indicating copy to clipboard operation
bicep copied to clipboard

Suggestion - provide standard constraint sets or types for parameters

Open NeilMacMullen opened this issue 5 years ago • 4 comments

I just decompiled a standard "website" ARM template and was rewarded with this code....

@minLength(1)
param hostingPlanName string

@allowed([
  'F1'
  'D1'
  'B1'
  'B2'
  'B3'
  'S1'
  'S2'
  'S3'
  'P1'
  'P2'
  'P3'
  'P4'
])
@description('Describes plan\'s pricing tier and capacity. Check details at https://azure.microsoft.com/en-us/pricing/details/app-service/')
param skuName string = 'F1'

I understand that the decompiler is just mechanically ripping this out of the ARM json but wouldn't it be a lot terser and human-friendly to be able to generate something like

@paramType AzureName
param hostingPlanName string

@paramType AzureWebSku
param skuName string = 'F1'

where AzureName and AzureWebSku are simply predefined in the tooling (and sourced from the actual Azure constraint schema).

NeilMacMullen avatar Apr 30 '21 10:04 NeilMacMullen

I'm pretty sure we have another issue for this, but I can't find it. We definitely want to do this and combine it with what we have planned for #622

Something like:

param skuName Web.Sites.Sku

Name is a little more complex as each resource type may have different naming requirements. So it is not possible to have a generic AzureName type, we could have something like Web.Sites.Name

@rynowak also has an interesting proposal in #1602 that would not require the parameter to be explicitly declared and we would have all they type info we need because we have the context for where it will be used.

alex-frankel avatar Apr 30 '21 16:04 alex-frankel

I like your syntax better than mine :-) I wasn't particularly hung up on AzureName - I'm painfully aware there are different conventions for different resources so you're dotted notation looks very nice. Of course, it's tempting to then think of this as a "type" and to prevent an Blob.Container.Name being passed to a Web resource but perhaps that kind of constraint checking is more easily done at run-time. In fact, given the amount of string-mangling done in the typical resource file, it might make more sense to go with the approach in #1602 and simply apply constraints to the final values being applied to properties rather than the input values of params.

NeilMacMullen avatar Apr 30 '21 16:04 NeilMacMullen

Thanks for your patience — this was recently picked up as an outstanding open issue for the Bicep team to review, and apologies for the long wait.

This has aged interestingly. Back in April 2021 @alex-frankel sketched param skuName Web.Sites.Sku and tied it to #622 (and @rynowak's contextual-constraints idea in #1602). Since then, a good chunk of the underlying capability has shipped: Bicep now has user-defined types and the ability to import types across files, so you can declare a constraint set once — e.g. a type with an @allowed([...]) union — and reuse it everywhere instead of copy-pasting the decompiled block you showed.

What's still not there is the part your suggestion was really about: first-party, Azure-schema-sourced type libraries (a canonical Web.Sites.Sku, region-name types, etc.) maintained by Microsoft. That's the durable remaining ask, and it overlaps with the broader type work in #622 and reusable-parameter requests like #18225.

I'm going to leave this open, reframed around that first-party-type-library gap. Feel free to reopen or chime in on #18225 if your scenario isn't covered.

torreymicrosoft avatar Jun 10 '26 18:06 torreymicrosoft

Thanks for your patience -- this was recently picked up as one of the older open issues for the ARM Deployments team to revisit.

User-defined types have shipped in Bicep and are exactly what you described. Instead of a long repeated @allowed([...]) block, declare a named union type once:

// Can live in a shared file and be imported with @export()
type AppServiceSkuName = 'F1' | 'D1' | 'B1' | 'B2' | 'B3' | 'S1' | 'S2' | 'S3' | 'P1' | 'P2' | 'P3' | 'P4'

param skuName AppServiceSkuName = 'F1'
param hostingPlanName string

This compiles cleanly on 0.46.1 (exit 0). The ARM JSON contains the full allowedValues array -- the constraint is enforced by the runtime. Put the type in a shared file and use import { AppServiceSkuName } from 'shared.bicep' so every team file references the same definition.

I'm going to close this out. If there is a constraint shape not expressible with union types or @minLength/@maxLength/@pattern decorators, please reopen and describe the specific scenario.

torreymicrosoft avatar Aug 18 '26 04:08 torreymicrosoft