I have two arrays: x
and y
I want to check if x
is contained within y
.
There is not contains() function in rego.
Next thing I tried is to create the mathematical equivalent to the contains() function:
x ⊆ y ⇔ (x ⋂ y) == x
.
So I tried to do the following:
z := x & y
x == z
But this creates the following error:
rego_type_error: and: invalid argument(s) have: (array[any], any, ???) want: (set[any], set[any], set[any])
So how can I do it?
Set operations only work on.. well, sets :) If you are creating x
and y
as new sets (just use {...}
instead of [...]
) your example should work. If x
and y
are arrays - like is often the case when referencing values from the input
- you can use a set comprehension to convert them, like:
xs := {e | e := x[_]}
ys := {e | e := y[_]}
And then use xs
and ys
as sets. If you on the other hand really want to use arrays (like when ordering and duplicate values matter), you could do something like this:
contained {
y[i]
x == array.slice(y, i, i + count(x))
}
This would iterate over the y
array, and for each item create a slice of y
from i
and to the same length as the x
array, and return true if one such slice is equal to x
.