Not sparse¶
notSparse validates that an array does not contain empty (sparse) slots.
It performs a strict structural array check and rejects any non‑array value. If the value is an array and contains at least one hole (an index that does not exist in the array), the rule emits a single validation event. Otherwise, it produces no validation output.
Signature¶
Through the API:
.notSparse();
And internally:
export const notSparse: ValidationRule
(value: unknown, path: FieldPath) => Promise<ReadonlyArray<JaneEvent>>
Events¶
| Event code | Description |
|---|---|
array.not.array |
Value is not structurally an array. |
array.is.sparse |
Array contains one or more empty (missing) slots. |
Design rationale¶
- Enforces strict, predictable validation that disallows sparse arrays.
- Uses explicit index‑existence checks (index in value) to detect holes.
- 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
JaneEventobjects.
Invoke¶
notSparse 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 any hole
(!(index in value)), emitsarray.is.sparse. - Otherwise, returns an empty result set.
Examples¶
Valid — no holes¶
await notSparse([1, 2, 3], "$");
// → []
Invalid — sparse array¶
const arr = [];
arr[2] = "x"; // holes at index 0 and 1
await notSparse(arr, "$");
// → [
// JaneEvent{
// kind: "error",
// code: "array.is.sparse",
// path: "$",
// ...
// }
// ]
Non‑array value¶
await notSparse("not-an-array", "$");
// → [
// JaneEvent{
// kind: "error",
// code: "array.not.array",
// path: "$",
// ...
// }
// ]