Skip to content

Non‑empty array

nonEmptyArray validates that an array contains at least one item.

It performs a strict structural array check and rejects any non‑array value. If the value is an array with zero items, the rule emits a single validation event. Otherwise, it produces no validation output.

Signature

Through the API:

.nonEmptyArray();

And internally:

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

Events

Event code Description
array.not.array Value is not structurally an array.
array.is.empty Array contains zero items.

Design rationale

  • Enforces strict, predictable validation that disallows empty 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

nonEmptyArray 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 with length 0, emits array.is.empty.
  • Otherwise → returns an empty result set.

Examples

Valid — at least one item

await nonEmptyArray([1], "$");
// → []

Invalid — empty array

await nonEmptyArray([], "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "array.is.empty",
//       path: "$",
//       ...
//     }
//   ]

Non‑array value

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