Skip to content

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, When selects resources and is emitted as written, and Require states the requirement and is inverted. Count -Where selects elements the same way.
  • No change to what Azure evaluates: the output is a count with a where, as now.
  • ConvertTo-Bylaw should write the readable form for the patterns it already converts.
  • Alpha: the language can still change, but the existing Each form stays valid.

Considered Options

  • -Where on Each
  • A When block inside Each
  • A None keyword, for "no element matches"
  • A Forbid block 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 matches S meets P. An element that doesn't match S is compliant, as is a missing or empty array.
  • -Where is the same as Count -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 as Must -Be 'Inbound'. -IfPresent and -Strict are compile errors there (BYL1010), an empty -Where is BYL1013, and AnyOf, AllOf and nested Count work inside. Each isn't allowed inside -Where (BYL1002): use a nested Count.
  • The block { P } is exactly an Each body today: inverted, with missing element fields compliant unless -Strict (ADR-0004), and nested Each, AnyOf, AllOf and Count allowed.
  • Field names in both blocks are relative to the element, and Element is the element itself.
  • -Where is 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 Each is: in Require, Each, AnyOf and AllOf, including the Require blocks 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, contains or containsKey), written into -Where as they are.
  • Requirement: the other conjuncts, inverted into the block, as the whole where is today.
  • exists stays in the requirement, since in a flattened where it 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 listed exists: true as 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 (-IfPresent or -Strict in -Where), BYL1013 (empty -Where), BYL1002 (Each in -Where), as for Count -Where. Current keeps its rules: -Where on Each is a field count's -Where, so Current with no name is the element, as in Count.
  • No new codes.

Consequences

  • Good, because the selection and the requirement are each written positively, and the AnyOf of negations disappears from the common case.
  • Good, because -Where means the same in Each and Count, and When and Require already 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": Each with negated statements, Each -Where with a requirement, and Count -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 -Where is 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 -Where in each place Each is allowed; nested; with AnyOf, AllOf, Count and Element in each block; each diagnostic. A golden test of the network security group example proves the Each -Where, AnyOf and Count forms equivalent.
  • The ALZ conversions are regenerated, and Deny-MgmtPorts-From-Internet reads with -Where. Every conversion is still proven.
  • The Array properties guide leads with Each -Where for "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 When is only at the top of a Resource today, and would need rules for where it may appear in a block and how often.
  • Bad, because Count already 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 0 under 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 Require a 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 AnyOf of negations stays the only way to write "each matching element must…" with Each's missing-field handling, and the guide can only explain it.

More Information