Documentation Index
Fetch the complete documentation index at: https://docs.cbrock.dev/llms.txt
Use this file to discover all available pages before exploring further.
is.function creates a guard that validates function signatures at runtime. It checks that a value is a callable, and optionally validates both the arguments passed to it and the value it returns without any changes to the function’s own source code.
The primary use case is wrapping an existing function with a validated shell using .impl (or one of its async/Result variants). The shell enforces the declared contract every time the function is called.
Creating a function guard
input(required) — an ordered tuple of guards, one per argument position.output(optional) — a guard for the return value. Omit it to allow any return type.
guard(v)) returns true if v is any function, and false otherwise. The input and output guards are not invoked during the predicate check.
.impl — synchronous validated wrapper
.impl wraps a function so that every call validates arguments first and the return value second. If validation fails, an AggregateGuardError is thrown.
args[N] for argument positions and return for the output:
.implAsync — async validated wrapper
Use .implAsync when the function returns a Promise. Inputs are validated synchronously (before the function runs). The resolved value is validated after the promise settles.
.implResult and .implResultAsync — non-throwing variants
If you prefer not to use exceptions for validation failures, use the Result-returning variants. They wrap the same logic but return Result<T, AggregateGuardError> instead of throwing.
ResultAsync<T, AggregateGuardError>:
Omitting output
When output is omitted, the return value is not validated and TypeScript infers its type from the implementation:
Using complex guards for inputs and outputs
Any guard expression works, including objects, arrays, unions, and chained helpers:Using is.function as a predicate
Because is.function produces a standard guard, it composes with all other guards. You can use it to validate that a field on an object is a function of a specific shape:
Error shape: AggregateGuardError
Thrown by .impl and .implAsync when validation fails. It is an Error subclass with two extra properties:
| Property | Type | Description |
|---|---|---|
.errors | GuardErr[] | All collected validation errors |
.schemaName | string | The guard’s inferred name |
.message property summarizes the first three errors inline. For structured error reporting you can iterate .errors directly:
Summary
| Method | Returns | Throws on failure |
|---|---|---|
.impl(fn) | ReturnType<fn> | Yes (AggregateGuardError) |
.implAsync(fn) | Promise<ReturnType<fn>> | Yes (rejected promise) |
.implResult(fn) | Result<T, AggregateGuardError> | No |
.implResultAsync(fn) | ResultAsync<T, AggregateGuardError> | No |
- Validate each argument against its position’s guard before calling the function
- Call the original function only if inputs pass
- Validate the return value against
output(if provided) before returning to the caller