Unique members in an array using YAML #2787
-
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. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@sapritchard In this case
In the future that could be supported by https://microsoft.github.io/PSRule/v2/expressions/functions/ but that isn't a currently supported scenario. I've added a feature request for future consideration. #2794 |
Beta Was this translation helpful? Give feedback.
@sapritchard In this case
.
is a literal. It's not referring to the current field. So, your YAML expression is requiring:Type.datacenter.Name
, the value must be any of these (.
), but don't allow any item to be repeated.In the future that could be supported by https://microsoft.github.io/PSRule/v2/expressions/functions/ but that isn't a currently supported scenario.
I've added a feature request for future consideration. #2794