Skip to content

unique ​

The array has no duplicate items.

Signature ​

ts
function unique<T>(by?: (item: T) => unknown, msg?: Message): Action<T[]>

Parameters ​

ParameterTypeDescription
by(item: T) => unknownWhat to compare by: (u) => u.email. Default — the item itself
msgMessageCustom error message

Error codes ​

CodeWhenDefault message
uniqueThere are duplicatesMust not contain duplicates

You can change the texts: see Messages & i18n.

Notes ​

Strings and numbers are compared by value, objects by content.

Example ​

ts
const Invite = object({
  emails: pipe(array(pipe(string(), email())), unique()),
  members: pipe(array(object({ email: string() })), unique((m) => m.email.toLowerCase())),
});

See also ​