This Commented CUE demonstrates how to make sure that a list of concrete, simple values only contains elements that are also present in another list. In other words, how to ensure that one list is a “subset” of another list (even though a list isn’t a set, strictly speaking).
file.cue
package example
_X: ["a", "b", "c"]
A: ["a", "a", "b", "a"]
// A must contain only values in _X.
A: [...or(_X)]
B: ["a", "b", "b", "E"]
// B must be a subset of _X.
B: [...or(_X)]
TERMINAL
$ cue vet
B.3: 3 errors in empty disjunction:
B.3: conflicting values "a" and "E":
./file.cue:3:6
./file.cue:9:20
./file.cue:11:5
B.3: conflicting values "b" and "E":
./file.cue:3:11
./file.cue:9:20
./file.cue:11:5
B.3: conflicting values "c" and "E":
./file.cue:3:16
./file.cue:9:20
./file.cue:11:5
This guide shows some lists of concrete and simple values being validated against another list.
The technique it demonstrates can also be used to validate incomplete (non-concrete) and composite (struct and list) values, but the rules around its use are nuanced and evolving.
Issue #2583 tracks some open questions about comparability in CUE that are worth considering before using this technique to validate more complex values.
Related content
- Issue #2583
- Reference: The CUE Language Specification: Comparison operators – CUE’s comparability rules