ConvertTo-Bylaw rewrites negated and [*] conditions into forms the language has, rather than the language gaining a Not group or bare [*] fields¶
Context and Problem Statement¶
Milestone 7 aims to need Condition (ADR-0040) less when converting the Azure Landing Zones Library (ALZ, ADR-0042). With the full alias snapshot, ALZ needs it for 27 conditions in 11 policy definitions (2026-11-29). 18 of them use not, so the roadmap proposed a Not { } group, or -Not on AnyOf and AllOf. How should Bylaw handle them?
Looking at the 18, none is a not around a group. Each wraps one condition, and they fall into three kinds:
| Kind | Count | Example | Already expressible as |
|---|---|---|---|
| A plain field | 3 | not(keySource equals 'Microsoft.Keyvault') |
the complementary operator, keySource notEquals 'Microsoft.Keyvault', which ConvertTo-Bylaw already writes |
A [*] field outside a count |
12, 5 of them inside another count's where |
not(destinationPortRanges[*] notEquals '*') |
Count destinationPortRanges[*] -Where { Element | Must -Be '*' } | Must -BeGreaterThan 0 |
| A value count | 3 | not(count(mandatoryTags where ...) equals length(...)) |
Count -Value ... | Must -Not -Be ...: Current already works in -Where (ADR-0032), so the not was all that was missing. Converted 2026-11-29 by inverting the count's comparison, since a count is never missing |
- Plain fields. Azure's operators come in complementary pairs,
equals/notEquals,in/notIn,contains/notContains,like/notLike,match/notMatch,containsKey/notContainsKey, andexists: true/exists: false, and on a single value each pair is exact, including for a missing field (one of the two is true; ADR-0004's truth table). Sonot(f op v)isf notOp v. The same holds for avaluecondition, which has no missing case at all, sonot(value op v)isvalue notOp v(added 2026-12-01, #91: 70 conditions in 11 of Azure's built-in policies). The ordering operators are kept as written, for fields and values alike. [*]fields. A condition ona[*]outside a count means "every element satisfies it", and is true for an empty array (Learn: referencing the array members collection; ADR-0002, ADR-0008 rule 6). Sonot(a[*] cond)means "some element breaks it", which is a count:count(a[*] where not cond) greater 0. For an empty array both are false. On a missing array, a[*]condition evaluates as its operator does on a missing field (measured 2026-11-29, ADR-0041), so when that is false, as forequalsorin, thenotholds there, and the count needs the array missing as an alternative:anyOf[ a exists false, count(...) greater 0 ]. It is nota[*] notCond: for["a", "b"],a[*] equals 'a'anda[*] notEquals 'a'are both false.
The language can already write the first two kinds: Must -Not and the operators, AnyOf/AllOf with De Morgan, and Count ... -Where. ConvertTo-Bylaw just doesn't rewrite a not into those forms, so it falls back to Condition.
One more finding. The semantic comparison that proves conversions (ADR-0041) turns every negated operator into not(positive). For a [*] field that is wrong, for the reason above, so it could call two different definitions equivalent. ConvertTo-Bylaw never writes a [*] field outside a count today, which is likely why no proof has gone wrong, but the rewrite for the second kind depends on the comparison getting exactly this right.
Decision Drivers¶
- Fewer
Conditions in converted policies, each conversion still proven equivalent. - The language stays small: no keyword without a case that needs it.
- The proof must be sound for
[*]fields before a rewrite relies on it. - A file says what it means. "Every element, and true when there are none" is easy to misread as "some element": authors often write
groupIds[*] equals xwhen they mean "contains x". - A statement means the same wherever it is written: moving it between
When,Requireand-Whereshouldn't change whether it compiles.
Considered Options¶
- Rewrite negated conditions in
ConvertTo-Bylaw, and fix the comparison for[*]fields - Add a
Not { }group to the language - Both
- For step 4 (added 2026-11-29): accept bare
[*]fields inWhenand-Where, emitted as written - For step 4: accept
Eachinside-Where, ascount(...) equals 0
Decision Outcome¶
Chosen option: "Rewrite negated conditions in ConvertTo-Bylaw, and fix the comparison for [*] fields", accepted 2026-11-29. ALZ has no case a Not group would serve that the language can't already write, so it waits for one. A not around a group, when one came up, needed no keyword either: see "A not around a group".
In order, as separate pull requests:
- Fix the comparison (ADR-0041). A field condition on a
[*]path outside a count becomes a quantified form, "no element breaks it": the same normal form ascount(a[*] where not cond) equals 0, soa[*] notEquals vis no longer taken asnot(a[*] equals v), and the two count forms compare equal. Tests:a[*] notEquals vandnot(a[*] equals v)are different;not(a[*] notEquals v)andcount(a[*] where a[*] equals v) greater 0are equivalent; the same inside a count'swhere. Done (ADR-0041, step 6). - Plain fields.
ConvertTo-Bylawpushes anotaround a single field condition into the complementary operator, then writes it as usual, for the equality operators andexists, whose complements the comparison folds; anotaround an ordering operator staysCondition. Done: 3 of the 18. [*]fields at the top level.not(a[*] cond)becomesCount a[*] -Where { <cond on the element, inverted> } | Must -BeGreaterThan 0. A path with a property after[*], such asa[*].b, usesField bin-Where; one that ends in[*]usesElement. Only when the snapshot has the array, so the language can count it; otherwise thenotis kept, forCondition. As a violation, the count is written asEachwhen its-Whereis the language's own, asConvertTo-Bylawwrites any such count. Done: 7 more.- Every
[*]field condition, at every array level, with or withoutnot. See below. 5 of the 18, and 3 more conditions without anot. Done: all 8, leaving 9Conditions in 5 policy definitions.
After steps 1 to 3, ALZ's Condition count should fall from 27 to 17, and Deny-MgmtPorts-From-Internet from 10 to 5. After step 2 it was 24, in 9 policy definitions, and after step 3 it is 17, in 8. The 3 value counts were converted afterwards by inverting the count's comparison, which leaves 1 Condition in all of ALZ.
A not around a group¶
Added 2026-11-30. ALZ has no not around a group, but a policy auditing VMs for BYOL licensing has one: not(anyOf[ licenseType equals Windows_Server, imageOffer contains byos, imagePublisher in [...], imagePublisher in (parameter) ]). ConvertTo-Bylaw wrote it with Condition. By De Morgan's laws it is allOf of the four nots, and each of those is a not around a single field, which step 2 writes as the complementary operator. So the decision stands: no Not { } keyword. ConvertTo-Bylaw rewrites not(anyOf[ ... ]) as allOf of the nots, and not(allOf[ ... ]) as anyOf, then rewrites each not as the other steps allow. A child they can't rewrite keeps its not, which is still exact. The comparison applies De Morgan's laws too (ADR-0041), so the proof is exact.
The policy's violation becomes one Require, its compliant form:
Require 'licenseType, imageOffer and imagePublisher are as required' {
AnyOf {
Field 'Microsoft.Compute/licenseType' | Must -Be 'Windows_Server'
Field 'Microsoft.Compute/imageOffer' | Must -ContainText 'byos'
Field 'Microsoft.Compute/imagePublisher' | Must -BeIn @('Canonical', 'Debian', 'OpenLogic')
Field 'Microsoft.Compute/imagePublisher' | Must -BeIn (Param AdditionalImages)
}
}
To keep it one Require, the if condition is split into its conjuncts as the input writes them, the type, When and the violation, before any rewrite. Each conjunct is rewritten on its own, so a rewrite that makes an allOf doesn't spread it across When. The same applies to the guard of step 4: allOf[ a exists true, count(...) equals 0 ] stays in its Require, as an AnyOf of Must -Not -Exist and the count.
Step 4: every [*] field condition¶
Amended 2026-11-29. Step 4 was planned as [*] fields inside a count's where, waiting for nested arrays. But the language already nests arrays: Count nests inside -Where, and Each and Count nest inside Each, with paths relative to the element (ADR-0032). BYL2005's fix already says "nest one Each or Count per level". So step 4 is step 3's rewrite, applied more widely. It isn't a language change.
What it covers. Of the 17 conditions left after step 3, 8 are a bare [*] field condition that no enclosing count iterates:
| Where | Count | Example |
|---|---|---|
In a count's where, under a second array |
5, all in Deny-MgmtPorts-From-Internet |
not(securityRules[*].destinationPortRanges[*] notEquals '*') inside count(securityRules[*] where ...) |
The same, without not |
2, in Deploy-Private-DNS-Generic |
privateLinkServiceConnections[*].groupIds[*] equals groupId inside count(privateLinkServiceConnections[*] where ...) |
At the top level, without not |
1, in Deny-Storage-ResourceAccessRulesTenantId |
networkAcls.resourceAccessRules[*].tenantId notEquals [subscription().tenantId] |
The rewrite. A field condition is rewritten when its path has a [*] beyond the arrays that enclosing counts iterate, with or without a not. Let a[*] be the path up to the first such [*], and c' the complement of the condition, the same field with the complementary operator, as in step 2:
a[*] ... condbecomescount(a[*] where c') equals 0, andnot(a[*] ... cond)becomescount(a[*] where c') greater 0.- When
cond's operator is false on a missing field, such asequals,in,like,containsorexists: true, the rewrite also tests the array, since a missing array gives the operator's missing-field value while its count is 0 (ADR-0041 step 6). Soa[*] ... condbecomesallOf[ a exists true, count(...) equals 0 ], andnot(a[*] ... cond)becomesanyOf[ a exists false, count(...) greater 0 ], as step 3 already writes. FornotEquals,notInand the others that are true on a missing field, the count alone is exact. - The new count iterates
a[*]. When the field has another[*]after it, as ina[*].b[*], itswhereisn'tc': on one element ofa[*]the condition is still about every element ofb[*], so the complementary operator would say "every one breaks it", not "some does". Thewhereisnot(cond)rewritten again, one level in, ascount(a[*].b[*] where c') greater 0. That gives one count per array level, at the top level or inside another count'swhere. - As in steps 2 and 3, only the equality operators and
existsare rewritten. The snapshot must have every array the rewrite counts, and the array itself where the guard tests it, so the language can name them. Otherwise the input is kept, forCondition, as written. - The result is written by the decompiler's existing forms. In a
Require, a violation count is written asEachwhen itswhereis the language's own. Elsewhere it is written asCount ... -Where { } | Must, withField a | Must -Existbeside it when there is a guard. A path ending in[*]usesElement, and one with a property after it usesField. For example, the Private DNS condition is anequals, so it is guarded:
Count 'privateLinkServiceConnections[*]' -Where {
Field privateLinkServiceId | Must -ContainText (Param ResourceType)
Field groupIds | Must -Exist
Count 'groupIds[*]' -Where {
Element | Must -Not -Be (Param GroupId)
} | Must -Be 0
} | Must -BeLessThan 1
The language is unchanged. A bare [*] field stays a compile error everywhere (BYL2005, ADR-0008 rule 6), including in When and -Where, where nothing is inverted. See the options below. BYL2005's suggested fix names the field after the array, so for a path ending in [*] it prints Field | Must. That is fixed to Element | Must.
Result. All 8 convert, as expected. The 2 in Deploy-Private-DNS-Generic use equals, so they are guarded. The other 6 use notEquals or notIn, so they are not. ALZ is then left with 9 Conditions in 5 policy definitions:
- 3 are a
notaround a value count. Converted since, by inverting the count's comparison:Currentalready worked in-Where. - 4 are
anyOf[ exists: false, notEquals ], a guard Bylaw never writes, sincenotEqualsis already true on a missing field. Converted since, by writing the parts on their own (ADR-0039). - 1 is
allOf[ exists: true, less ], a guard Bylaw never writes either, sincelessis already false on a missing field. Converted since, in the same way. - 1 compares a string that looks like an expression,
current(...)without brackets, which Azure reads as text.
Consequences¶
- Good, because the conversions use the language people already write, and stay proven.
- Good, because the comparison becomes sound for
[*]fields, which matters whatever the decompiler does. - Neutral, because an author who thinks in
notwritesMust -Not, or De Morgan withAnyOfandAllOf, as today. - Neutral, because a
notaround a whole group needs no keyword either: see "A not around a group" below. - Good, because a converted
[*]condition says what "every element" means, as a count of the elements that break it, with the array required to exist where Azure requires it. So the empty and missing cases are visible rather than implied. - Bad, because a converted
[*]condition is longer than the input, especially when nested.
Confirmation¶
Unit tests for the comparison's [*] forms, the decompiler's rewrites, and the ALZ baseline recording the improvement (ADR-0042). For step 4, the unit tests also cover:
- the rewrite with and without
not, at the top level, in a count'swhere, and under two arrays at the top level; - the guard, present for an operator that is false on a missing field and absent for one that is true there;
- a field the snapshot lacks, which stays
Conditionas written; - the BYL2005 message for a path ending in
[*].
Pros and Cons of the Options¶
Add a Not { } group to the language¶
- Good, because it reads as written for a
notaround a group. - Bad, because none of ALZ's 18 is a
notaround a group, so it removes none of them as written: each would still need the rewrites above to be idiomatic. - Bad, because it adds a second way to say what
Must -Notand De Morgan already say, with its own missing-field rules to define and test.
Both¶
- Good, because it covers a future
notaround a group. - Bad, because it builds the keyword before anything needs it.
Accept bare [*] fields in When and -Where, emitted as written¶
- Good, because the conversion is literal and short.
- Bad, because the same statement would compile in
Whenand fail inRequire, which breaks the rule that a statement means the same wherever it is. - Bad, because the implicit "every element, true when empty, the operator's value when missing" is the meaning authors most often get wrong, and Bylaw would no longer catch it.
Accept Each inside -Where¶
- Good, because
Eachreads well for "every element". - Bad, because
Eachis a requirement, inverted and guarded, while-Whereis written as is. ADR-0032 keeps them apart for that reason, andCount ... | Must -Be 0already says it. - Bad, because it covers only the forms without
not.
More Information¶
- Author policies for array properties:
[*]field conditions outside a count, and field count expressions. - Policy rule: count: nested counts, and the current function.
- build/verify-array-fields: how
[*]conditions evaluate on empty and missing arrays, measured 2026-11-29. - ADR-0002 (desired state and
[*]outsideEach), ADR-0004 (missing fields), ADR-0032 (Count), ADR-0039 (ConvertTo-Bylaw), ADR-0040 (Condition), ADR-0041 (semantic comparison), ADR-0042 (ALZ coverage).