In CUE, you often will work with lists of all sorts of values. To ensure a list has no duplicate items, use list.UniqueItems.

Strings

CUE
import "list"

items: ["a", "b", "c", "a"]
items: list.UniqueItems

Integers

CUE

import "list"

items: [1, 2, 3, 1]
items: list.UniqueItems

Structs

A common issue is we have a list of structs, each with some key that must be unique across all items in the list.

To ensure a list of structs has no duplicate keys, one common approach is to guarantee that the list has no duplicate items by constructing the list from a map.

CUE
_items: {
	joe: age: 30
	alice: age: 35
}
_items: [name=string]: "name": name

items: [
	for item in _items {
		item
	}
]

If the list must be constrained directly, you can write an auxiliary field that creates a mapping from the keys

CUE
items: [
	{
		name: "joe"
		age: 30
	},
	{
		name: "alice"
		age: 35
	},
	{
		name: "joe"
		age: 31
	}
]
_itemsCheck: {
	for i, item in items {
		(item.name): i
	}
}

If the key is specified twice, there will be a conflict in _itemsCheck.

Ensuring multiple keys to have no duplicates

If there are multiple keys that together must be unique, a similar approach can be used, using json.Marshal to form a composite string key from the keys. Here, for example, the combination of name and dateOfBirth must be unique:

CUE
import "encoding/json"

items: [
	{
		name: "joe"
		dateOfBirth: "1983-10-21"
		country: "US"
	},
	{
		name: "alice"
		dateOfBirth: "1987-10-15"
		country: "DE"
	},
	{
		name: "joe"
		dateOfBirth: "2010-02-05"
		country: "UK"
	},
	{
		name: "alice"
		dateOfBirth: "1987-10-15"
		country: "BE"
	},
]
_itemsCheck: {
	for i, item in items {
		(json.Marshal([item.name, item.dateOfBirth])): i
	}
}