function withValidationMetadata
Since 0.3.0
withValidationMetadata<T extends MiddlewareFunction>(
middleware: T,
): T

Brands a middleware function with the request part and schema it validates, so a documentation generator can read it without importing the plugin that produced it.

The function is branded in place and returned, so identity is preserved and the brand costs no wrapper frame per request. The property is symbol-keyed and non-enumerable, so it is invisible to Object.keys, JSON.stringify and spread, and the middleware behaves exactly as it did.

Examples

Example 1

export function validateBody(schema: unknown): MiddlewareFunction {
  return withValidationMetadata(async (ctx, next) => {
    // ... validate ctx.request body against `schema` ...
    await next();
  }, { target: 'body', schema });
}

Type Parameters

Parameters

middleware: T

The middleware to brand

The request part it validates, and against what

Return Type

The same middleware reference, branded

Usage

import { withValidationMetadata } from "common/src/index.ts";