Includes¶
includes validates that an array contains a specific required item.
It performs a strict structural array check and rejects any non‑array value. If the value is an array and does not contain the required item (using JavaScript’s Array.prototype.includes semantics), the rule emits a single validation event. Otherwise, it produces no validation output.
Signature¶
Through the API:
.includes(item)
And internally:
export const includes: (item: unknown) => ValidationRule
(value: unknown, path: FieldPath) => Promise<ReadonlyArray<JaneEvent>>
Events¶
| Event code | Description |
|---|---|
array.not.array |
Value is not structurally an array. |
array.is.missing-value |
Array does not contain the required item. |
Design rationale¶
- Enforces strict, predictable inclusion of a specific item in an array.
- Uses JavaScript
Array.prototype.includessemantics (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
JaneEventobjects.
Invoke¶
includes 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 does not include the required item, emits
array.is.missing-value. - Otherwise, returns an empty result set.
Examples¶
Valid — item present¶
const rule = includes("x");
await rule(["a", "x", "b"], "$");
// → []
Invalid — item missing¶
const rule = includes(42);
await rule([1, 2, 3], "$");
// → [
// JaneEvent{
// kind: "error",
// code: "array.is.missing-value",
// path: "$",
// ...
// }
// ]
Non‑array value¶
const rule = includes("x");
await rule("not-an-array", "$");
// → [
// JaneEvent{
// kind: "error",
// code: "array.not.array",
// path: "$",
// ...
// }
// ]