@intuitionrobotics/thunderstorm
Version:
37 lines • 1.09 kB
JavaScript
import { validate } from "@intuitionrobotics/ts-common";
import {} from "express";
/**
* Body-validation middleware. Runs the same ts-common `validate()` the legacy
* `ServerApi` used (server-api.ts:164), forwarding a `ValidationException` to
* the app-level `terminalErrorHandler` (which maps it to 400 via
* `composeApiError`, identical to today) through `next(err)`.
*
* Compose it at the route site: `router.post("/x", validateBody(V), handler(fn))`.
*/
export function validateBody(validator) {
return (req, _res, next) => {
try {
validate(req.body, validator);
next();
}
catch (e) {
next(e);
}
};
}
/**
* Query-validation middleware. Uses Express-parsed `req.query` (the raw model
* drops the old manual `parse(req.url, true).query`).
*/
export function validateQuery(validator) {
return (req, _res, next) => {
try {
validate(req.query, validator);
next();
}
catch (e) {
next(e);
}
};
}
//# sourceMappingURL=validators.js.map