Skip to content

Unique members in an array using YAML #2794

Open
@BernieWhite

Description

@BernieWhite

Investigate support for using functions for this case. See discussion for details.

Discussed in #2787

Originally posted by sapritchard March 3, 2025
Hi, I'm looking to see if I can iterate over an array to ensure the members are unique using a YAML rule file. I can achieve the result using PowerShell but would like to understand where I am going wrong using YAML. My PowerShell version of the rule is:

Rule 'Unique Members' {
    $array_members = @($TargetObject.Type.datacenter.Name)
    $Assert.Subset($TargetObject, 'Type.datacenter.Name', $array_members, $null, $True)
}

I've tried a few variations with a YAML rule, I think the below is close:

---
# Synopsis: Each member of the Name array is unique
apiVersion: github.com/microsoft/PSRule/v1
kind: Rule
metadata:
  name: datacenter.Name.member.unique
spec:
  condition:
    field: Type.datacenter.Name
    subset:
      - .
    unique: true

My configuration is in JSON:

{
  "Type": {
    "datacenter": {
      "Name": [
        "DEV_LAB",
        "DEV_LAB1"
	]
    }
  }
}

My result:

  FAIL  datacenter.Name.member.unique

  Each member of the Name array is unique

  File: datacenter.json:2:8

  Reason:
  - Path Type.datacenter.Name: The field 'Type.datacenter.Name' did not contain '.'.

Again I'm sure it's just me not interpreting the documentation correctly. Appreciate any help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature: languageIssues that affect language such keywords and variables

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions