Documentation
    Preparing search index...

    Function predicate

    • Validates that a given condition evaluates to true.

      Supports synchronous boolean values, synchronous functions returning boolean, and asynchronous functions returning Promise. The return type is automatically inferred based on the input type.

      Processing cost: The condition is evaluated once and asynchronous settlement adds constant bookkeeping without polling or repeating the condition.

      Reuse: Each invocation must evaluate its supplied task or request to observe current behavior; prior results cannot replace these effectful checks.

      Resource ownership: The returned promise retains invocation-local state until settlement. The callback owns its external resources, and this validator provides no cancellation or timeout.

      Type Parameters

      • T extends boolean | (() => boolean) | (() => Promise<boolean>)

      Parameters

      • title: string

        Descriptive title used in error messages when validation fails

      • condition: T

        The condition to validate (boolean, function, or async function)

      Returns T extends () => Promise<boolean> ? Promise<void> : void

      Void or Promise based on the input type

      // Synchronous boolean
      TestValidator.predicate("user should exist", user !== null);

      // Synchronous function
      TestValidator.predicate(
      "array should be empty",
      () => arr.length === 0,
      );

      // Asynchronous function
      await TestValidator.predicate(
      "database should be connected",
      async () => await db.ping(),
      );

      Error with descriptive message when condition is not satisfied

      contracts/common.md#principled-implementation A boolean, a function returning a boolean, or a function returning a promise of a boolean is evaluated, and only the exact value true passes; a false result throws the same Error in all three forms, and the asynchronous form rejects with it.

      contracts/common.md#clear-and-simple-design One function whose three branches differ only in when the value is available.

      contracts/common.md#prohibited-implementation-shortcuts The check is on the value the caller gives, with no truthiness coercion.

      contracts/common.md#meaningful-documentation The comment documents the three forms, the parameters, the return type rule, and the thrown error, with examples.