Skip to content

Has unique items

hasUniqueItems validates that an array contains only unique items.

It performs a strict structural array check and rejects any non‑array value. If the value is an array and contains duplicate items (as determined by JavaScript’s Set equality semantics), the rule emits a single validation event. Otherwise, it produces no validation output.

Signature

Through the API:

.hasUniqueItems()

And internally:

export const hasUniqueItems: ValidationRule
(value: unknown, path: FieldPath) => Promise<ReadonlyArray<JaneEvent>>

Events

Event code Description
array.not.array Value is not structurally an array.
array.not.unique Array contains duplicate items.

Design rationale

  • Enforces strict, predictable uniqueness validation for arrays.
  • Uses JavaScript Set semantics for equality (value equality for primitives, reference equality for objects).
  • Rejects non‑array values with a structural‑type diagnostic.
  • Emits exactly one event per failure for clarity and composability.
  • Never coerces, normalizes, or transforms — validation is explicit and opt‑in.
  • Async‑compatible and returns a readonly array of JaneEvent objects.

Invoke

hasUniqueItems runs only when explicitly included in a boundary or pipeline. It does not run automatically.

The rule activates when:

  • The value is any JavaScript value.
  • If the value is not an array, emits array.not.array.
  • If the value is an array and contains duplicates, emits array.not.unique.
  • Otherwise, returns an empty result set.

Examples

Valid — all items unique

await hasUniqueItems([1, 2, 3], "$");
// → []

Invalid — duplicates present

await hasUniqueItems([1, 2, 2], "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "array.not.unique",
//       path: "$",
//       ...
//     }
//   ]

Non‑array value

await hasUniqueItems("not-an-array", "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "array.not.array",
//       path: "$",
//       ...
//     }
//   ]