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.

  // 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

  • Parameters

    • title: string

      Descriptive title used in error messages when validation fails

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

    A currying function that accepts the condition to validate