This Commented CUE
demonstrates how to use the built-in functions
list.MaxItems
and
list.MinItems
to require that a list contains a maximum and/or minimum number of items.
file.cue
package example
import "list"
a: [1, 2, 3, 4, 5]
b: [1, 2, 3, 4, 5]
c: [1, 2, 3, 4, 5]
// a must contain no more than 2 items
a: list.MaxItems(2)
// b must contain at least 6 items
b: list.MinItems(6)
// c must contain at least 2 items, and no more than 6 items
c: list.MinItems(2) & list.MaxItems(6)
TERMINAL
$ cue vet
a: invalid value [1,2,3,4,5] (does not satisfy list.MaxItems(2)): len(list) > MaxItems(2) (5 > 2):
./file.cue:10:4
./file.cue:5:4
./file.cue:10:18
b: invalid value [1,2,3,4,5] (does not satisfy list.MinItems(6)): len(list) < MinItems(6) (5 < 6):
./file.cue:13:4
./file.cue:6:4
./file.cue:13:18
Alternative to list.MinItems
If the number of required items is both small and known in advance, then this
core language syntax might be preferred instead of list.MinItems
:
file.cue
package example
// f must contain at least 3 elements
f: [_, _, _, ...]
f: [1, 2]
TERMINAL
$ cue vet
f: incompatible list lengths (2 and 3)
Related content
- The
list
built-in package