Count is piped to Must, for field counts and value counts¶
Context and Problem Statement¶
Each covers one kind of count: every element of an array meets a condition, emitted as count(where: violation) greater 0. Azure Policy can do more with count, and the Azure Landing Zones Library uses it. Of the 67 ALZ definitions whose effects Bylaw supports, 11 use conditions Bylaw can't express:
- 7 count an array without
where: "the array is empty", "has at least one element". - 3 use a value count: they count the members of a list, such as a parameter of mandatory tags, that meet a condition, using
current(). - 1 uses a
[*]field outside a count, which Bylaw rejects (BYL2005) because negating it isn't safe.
Learn also documents comparing a count with other numbers: "exactly one", "at least two", or the length of a parameter. How are these written?
Decision Drivers¶
- A count is a number, so it should be tested with the same operators as other numbers.
- What to count is a selection, not a requirement, so it is written as is, like
When. Eachstays the simple way to say "every element".
Considered Options¶
- A
Countkeyword whose result is piped toMust - More
Eachvariants:EachOf,AnyOfover elements,None - Only the planned raw-condition escape hatch
Decision Outcome¶
Chosen option: "A Count keyword whose result is piped to Must".
Require 'At least one IP rule' {
Count networkAcls.ipRules[*] | Must -BeGreaterThan 0
}
Require 'At most two rules allow inbound traffic' {
Count securityRules[*] -Where {
Field direction | Must -Be 'Inbound'
Field access | Must -Be 'Allow'
} | Must -BeLessOrEqual 2
}
Require 'The name matches an allowed pattern' {
Count -Value (Param NamePatterns) -Name pattern -Where {
Field name | Must -BeLike (Current pattern)
} | Must -BeGreaterThan 0
}
Count¶
Count path[*]is a field count, emitted as{ "count": { "field": "<alias>[*]" } }.Count -Value <array> [-Name <name>]is a value count. The array is a literal, aParamor anArmexpression.-Where { }selects the elements to count, emitted ascount.where. Without it, every element is counted. Statements in-Whereare emitted as written, likeWhen: they select elements, they aren't requirements, so they aren't inverted and get no missing-field guards. As inWhen,-IfPresentand-Strictare compile errors there.AnyOf,AllOfand nestedCountwork inside.Eachisn't allowed inside-Where: use a nestedCount.- In a field count's
-Where, field names are relative to the element, as inEach, andElementis the element itself. Countis piped toMustlike a value condition (ADR-0025). InRequirethe assertion is inverted and never guarded, since a count is never missing. InWhenit is emitted as written. The operators are-Be,-BeInand the four comparisons. Others are a compile error.Countworks inWhen,RequireandEach, and in an existence condition (ADR-0030). InsideEach, a field count's path is relative to the element, which gives the nested counts Learn allows.
Current¶
Current nameis[current('name')], the member a value count is at.Currentwith no name is[current()], allowed only when the count isn't inside another count, as Learn requires. In a field count's-Where,Currentis[current('<array alias>[*]')].Currentis allowed only inside a-Where. It can be used as a value, as aResourceGroupTagname, and as the subject of a value condition. It can't name aTag: see Details.-Nameis letters and digits. Learn makes it required when the count is inside another count. Without it, Azure usesdefault.
Limits¶
- At most 10 value counts per rule, and at most 100 iterations for a literal array, nested counts included, as Learn documents. Exceeding either is a compile error. The existing limit of 5 field counts per array (BYL3050) also counts
Count.
Relationship to Each and [*] fields¶
Each path[*] { P }stays, and is the same asCount path[*] -Where { violation of P } | Must -Be 0. The README shows both.- A
[*]field outsideEachorCountstays a compile error (BYL2005). Its message now suggestsCount.
Details¶
- A count is compared with whole numbers: literals, an
Integerparameter (anArrayone for-BeIn), the length of a parameter withParam Name -Length(ADR-0020), orArm. Current namerefers to the value count with that-Namearound it, so a nested count can use an outer count's member. ACount -Valueinside another count, including insideEach, needs-Name.ResourceGroupTag (Current name)emits[resourceGroup().tags[current('name')]], and needs a value count.Elementin-Whereis only for a field count; a value count's member isCurrent.
Diagnostics¶
- Front end: BYL1047 (neither or both of a path and
-Value, a path without[*], or a-Valuethat is not a list), BYL1048 (-Nameon a field count, or not letters and digits), BYL1049 (a nested value count without-Name), BYL1050 (Currentoutside-Where, with a name no count has, or without a name in a nested value count), BYL1051 (an operator a count doesn't take, or-IfPresentor-Stricton a count), BYL1052 (Countused as a value).-IfPresentand-Strictin-Whereare BYL1010, an empty-Whereis BYL1013, andEachin-Whereis BYL1002.Tag (Current)is BYL1059. - Validation: BYL3100 (a count compared with something other than a whole number), BYL3101 (a
-Valueparameter that is not anArray), BYL3052 (more than 10 value counts in a rule), BYL3053 (more than 100 iterations over literal lists, nested counts multiplied).
Consequences¶
- Good, because every count form Learn documents can be written, and tested with the familiar operators.
- Good, because
Eachstays simple for the common case. - Bad, because
-Whereis written as is, unlikeRequire. Its name says it selects, and the README explains the difference.
Confirmation¶
Unit tests cover field and value counts with and without -Where, each operator in When and Require, nested counts, Current in each form and place, and the limits. Golden tests cover the ALZ patterns: an empty-array check, a value count over a parameter with Current as a value, and a nested count.
Pros and Cons of the Options¶
Count piped to Must¶
- Good, because one keyword covers every count form.
- Bad, because there are now two ways to say "every element".
More Each variants¶
- Good, because each pattern gets a readable name.
- Bad, because there is a keyword per pattern, and comparisons with numbers other than 0 still aren't covered.
Escape hatch only¶
- Good, because there is nothing new to learn.
- Bad, because counts are common enough that raw JSON would be the usual way to write them, with no validation.