Skip to content

Items equal

itemsEqual validates that an array contains exactly the specified number of items.

It performs a strict structural array check and rejects any non‑array value. If the value is an array whose length does not match the required count, the rule emits a single validation event. Otherwise, it produces no validation output.

Signature

Through the API:

.itemsEqual(exact)

And internally:

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

Events

Event code Description
array.not.array Value is not structurally an array.
array.has.invalid-length Array length does not match the required count.

Design rationale

  • Enforces strict, predictable exact‑length validation for arrays.
  • 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

itemsEqual runs only when explicitly included in a boundary or pipeline. It does not run automatically and is disabled when strict mode is enabled.

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 whose length is not exactly exact, emits array.has.invalid-length.
  • Otherwise, returns an empty result set.

Examples

Valid — exact number of items

const rule = itemsEqual(3);

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

Invalid — too few or too many items

const rule = itemsEqual(2);

await rule([1], "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "array.has.invalid-length",
//       path: "$",
//       ...
//     }
//   ]
await rule([1, 2, 3], "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "array.has.invalid-length",
//       path: "$",
//       ...
//     }
//   ]

Non‑array value

const rule = itemsEqual(1);

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