Skip to content

Excludes

excludes validates that an array does not include a specific disallowed item.

It performs a strict structural array check and rejects any non‑array value. If the value is an array and contains the disallowed 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:

.excludes(item)

And internally:

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

Events

Event code Description
array.not.array Value is not structurally an array.
array.has.invalid-value Array contains the disallowed item.

Design rationale

  • Enforces strict, predictable exclusion of a specific item from an array.
  • Uses JavaScript reference equality (Array.prototype.includes) for object comparison.
  • 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

excludes 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 includes the disallowed item, emits array.has.invalid-value.
  • Otherwise, returns an empty result set.

Examples

Valid — item not present

const rule = excludes("x");

await rule(["a", "b", "c"], "$");
// → []

Invalid — item present

const disallowed = { id: 1 };
const rule = excludes(disallowed);

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

Non‑array value

const rule = excludes(42);

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