Skip to content

SudoSOS Back-end API / internal/middleware / AsyncValidatorMiddleware

Class: AsyncValidatorMiddleware ​

Middleware that runs async (potentially DB-hitting) validation specs against the request body before passing to the handler. Runs after RequestValidatorMiddleware (structural Swagger check) and returns the same { valid, errors[] } shape on failure, ensuring a consistent 400 response regardless of which layer caught the error.

If no spec is registered in the registry for the given model name, the middleware is a no-op and calls next() immediately.

Constructors ​

Constructor ​

ts
new AsyncValidatorMiddleware(registry, validator): AsyncValidatorMiddleware;

Creates a new async validator middleware instance.

Parameters ​

ParameterTypeDescription
registryAsyncValidatorRegistryThe registry to look up specs from.
validatorBodyValidatorThe BodyValidator containing the model name to look up.

Returns ​

AsyncValidatorMiddleware

Methods ​

getMiddleware() ​

ts
getMiddleware(): RequestHandler;

Returns ​

RequestHandler

a middleware handler to be used by express.


handle() ​

ts
handle(
   req, 
   res, 
next): Promise<void>;

Middleware handler. Looks up the spec for the model name and runs it against req.body.

Parameters ​

ParameterTypeDescription
reqRequestWithTokenthe express request to handle.
resResponsethe express response object.
nextFunctionthe express next function to continue processing of the request.

Returns ​

Promise<void>