violation[{"msg": msg}] {
required:= ["red", "green", "blue"]
input := ["orange", "purple"]
msg := sprintf("")
}
I want to compare each value from the input array in the required array. In other languages, two normal loops will do it. but in the Rego language, they are no loops. Does anybody know how can I do it
See the docs section on iteration for how to iterate over a collection. It is however often more idiomatic to work with sets. Following your example:
violation[{"msg": msg}] {
required := {"red", "green", "blue"}
input.colors := {"orange", "purple"}
count(required - input.colors) != 0
msg := sprintf("input.colors (%v) does not contain all required colors (%v), [input.colors, required]")
}
colorSet and requiredSet variable shows how to convert array to set.
Using ==
operator we can figure out if all colors are present or not
package play
default allColorPresent = false
allColorPresent {
colorSet := {x | x := input.colors[_]}
requiredSet := {x | x := input. required[_]}
colorSet == requiredSet
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With