Each takes a -Where block that selects the elements its requirement applies to¶
Context and Problem Statement¶
Each path[*] { P } says every element meets P (ADR-0002), with a missing or empty array compliant and missing element fields lenient (ADR-0004). Many array requirements apply to some elements only: "every inbound rule that allows traffic must not come from the internet". Each has nowhere to say which elements, so the selection has to be negated and joined to the requirement with AnyOf (ADR-0026):
Require 'No rule allows inbound traffic from the internet' {
Each securityRules[*] {
AnyOf {
Field direction | Must -Not -Be 'Inbound'
Field access | Must -Not -Be 'Allow'
Field sourceAddressPrefix | Must -Not -BeIn '*', 'Internet'
}
}
}
This is correct, but hard to read: the author thinks "inbound AND allow AND from the internet is forbidden", and has to write "not inbound OR not allow OR not from the internet". Using AllOf, the natural-looking choice, gives a policy that rejects almost every network security group. The Azure Landing Zones Library's Deny-MgmtPorts-From-Internet converts to the same shape, with an AllOf of further negations inside the AnyOf.
The same requirement can be written as Count securityRules[*] -Where { <the forbidden rule> } | Must -Be 0 (ADR-0032), which reads as written. But that only covers "no element matches": it doesn't apply Each's missing-field handling to the requirement, and it can't say "each matching element must meet a requirement" when the requirement itself needs the compliant-state form, such as a nested Each, -Strict, or several statements that must all hold.
How should Each say which elements a requirement applies to?
Decision Drivers¶
- Authors write what must be true (ADR-0002), and the selection should be written as positively as the requirement.
- Reuse a rule authors already know: at the policy level,
Whenselects resources and is emitted as written, andRequirestates the requirement and is inverted.Count -Whereselects elements the same way. - No change to what Azure evaluates: the output is a
countwith awhere, as now. ConvertTo-Bylawshould write the readable form for the patterns it already converts.- Alpha: the language can still change, but the existing
Eachform stays valid.
Considered Options¶
-WhereonEach- A
Whenblock insideEach - A
Nonekeyword, for "no element matches" - A
Forbidblock that describes the violation directly - Documentation only
Decision Outcome¶
Chosen option: "-Where on Each", because it splits the selection from the requirement in the same way When and Require do, uses the name Count already uses for selecting elements, and compiles to the count Bylaw already emits.
Require 'No rule allows inbound traffic from the internet' {
Each securityRules[*] -Where {
Field direction | Must -Be 'Inbound'
Field access | Must -Be 'Allow'
} {
Field sourceAddressPrefix | Must -Not -BeIn '*', 'Internet'
}
}
It reads as "each inbound rule that allows traffic must not come from the internet".
Meaning¶
Each path[*] -Where { S } { P }means every element that matchesSmeetsP. An element that doesn't matchSis compliant, as is a missing or empty array.-Whereis the same asCount -Where(ADR-0032): statements are emitted as written, not inverted, with no missing-field guards. So an element without the field doesn't match a positive selection such asMust -Be 'Inbound'.-IfPresentand-Strictare compile errors there (BYL1010), an empty-Whereis BYL1013, andAnyOf,AllOfand nestedCountwork inside.Eachisn't allowed inside-Where(BYL1002): use a nestedCount.- The block
{ P }is exactly anEachbody today: inverted, with missing element fields compliant unless-Strict(ADR-0004), and nestedEach,AnyOf,AllOfandCountallowed. - Field names in both blocks are relative to the element, and
Elementis the element itself. -Whereis optional.Each path[*] { P }is unchanged.
Emitted JSON¶
Each in a Require emits the count of the elements that break it, greater than 0 (ADR-0002). With -Where, an element breaks it when it matches S and breaks P:
{ "count": { "field": "<alias>[*]",
"where": { "allOf": [ <S, as written>, <violation of P> ] } },
"greater": 0 }
With several statements in -Where, each is a conjunct of the allOf, which is flattened as Bylaw already does. So the example above emits the same rule as the AnyOf form: Bylaw's comparison (ADR-0041) proves the two equivalent.
- It is still one field count on the array, so the limit of 5 per array (BYL3050) counts it once.
- It is allowed wherever
Eachis: inRequire,Each,AnyOfandAllOf, including theRequireblocks of an existence condition (ADR-0030).
ConvertTo-Bylaw¶
Where the decompiler writes an Each for a violation count(a[*] where W) greater 0 today (ADR-0039, ADR-0045), and W is an allOf, it splits the conjuncts:
- Selection: conjuncts that are a field or value condition with a positive operator (
equals,in,like,match,matchInsensitively,containsorcontainsKey), written into-Whereas they are. - Requirement: the other conjuncts, inverted into the block, as the whole
whereis today. existsstays in the requirement, since in a flattenedwhereit is usually part of a missing-field guard, and so does a condition on a field the requirement also tests, so that a field's conditions stay together. Without this, a guarded requirement would be split into-Where { Field x | Must -Exist } { Field x | Must -Be … -Strict }, which is equivalent but harder to read. (Amended 2026-12-01, during implementation: the first draft listedexists: trueas a selection.)
It writes -Where only when both parts are non-empty, and both can be written without Condition; otherwise it writes the Each as before. When every conjunct is a selection, it writes Count a[*] -Where { ... } | Must -Be 0, which reads as written. Every conversion is still proven equivalent (ADR-0041), so the split can only change how the file reads, not what it means.
A count compared with "at most 0" or "less than 1" is written | Must -Be 0, since a count is never negative (ADR-0041 step 5). This applies to every converted count, not only those from this split: 15 counts in the Azure Landing Zones Library read | Must -Be 0 instead of | Must -BeLessOrEqual 0 or | Must -BeLessThan 1.
Diagnostics¶
- Front end: BYL1010 (
-IfPresentor-Strictin-Where), BYL1013 (empty-Where), BYL1002 (Eachin-Where), as forCount -Where.Currentkeeps its rules:-WhereonEachis a field count's-Where, soCurrentwith no name is the element, as inCount. - No new codes.
Consequences¶
- Good, because the selection and the requirement are each written positively, and the
AnyOfof negations disappears from the common case. - Good, because
-Wheremeans the same inEachandCount, andWhenandRequirealready teach the as-written and inverted split. - Good, because the output and the proof are unchanged, so there is no new risk in what Azure evaluates.
- Bad, because there are three ways to say "no element matches":
Eachwith negated statements,Each -Wherewith a requirement, andCount -Where | Must -Be 0. The Array properties guide shows which to use. - Bad, because the selection and the requirement differ on missing fields: a missing field in
-Whereis tested as Azure tests it, so it doesn't match a positive selection, and a missing field in the block is compliant unless-Strict. Both usually lead to compliant, but the guide must say why.
Confirmation¶
- Unit tests:
Each -Wherein each placeEachis allowed; nested; withAnyOf,AllOf,CountandElementin each block; each diagnostic. A golden test of the network security group example proves theEach -Where,AnyOfandCountforms equivalent. - The ALZ conversions are regenerated, and
Deny-MgmtPorts-From-Internetreads with-Where. Every conversion is still proven. - The Array properties guide leads with
Each -Wherefor "each matching element must…".
Pros and Cons of the Options¶
-Where on Each¶
- Good, because it is the same parameter as on
Count, with the same meaning. - Good, because it is optional, so existing files don't change.
- Neutral, because two script blocks in a row,
-Where { } { }, need the guide to show the layout.
A When block inside Each¶
Each securityRules[*] {
When {
Field direction | Must -Be 'Inbound'
Field access | Must -Be 'Allow'
}
Field sourceAddressPrefix | Must -Not -BeIn '*', 'Internet'
}
- Good, because it mirrors
Resource { When { } Require { } }. - Bad, because
Whenis only at the top of aResourcetoday, and would need rules for where it may appear in a block and how often. - Bad, because
Countalready selects elements with-Where, so there would be two ways to select elements.
A None keyword¶
- Good, because "no element matches all of these" reads directly.
- Bad, because it is
Count ... -Where { } | Must -Be 0under another name, which ADR-0032 already chose not to add. - Bad, because it doesn't cover "each matching element must meet a requirement".
A Forbid block¶
- Good, because the violation is written as the author thinks of it.
- Bad, because it reverses the missing-field rule: in
Requirea missing field is a violation, but in a block that describes the violation, a missing field matches nothing, so it is compliant. Two models of missing fields in one language is what ADR-0004 avoids.
Documentation only¶
- Good, because nothing changes.
- Bad, because the
AnyOfof negations stays the only way to write "each matching element must…" withEach's missing-field handling, and the guide can only explain it.