mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-23 15:21:46 +00:00
refactoring: TypeScript migration, security fixes,
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
|
||||
function maxLength(field: string, max: number) {
|
||||
return (req: Request, res: Response, next: NextFunction): void => {
|
||||
if (req.body[field] && typeof req.body[field] === 'string' && req.body[field].length > max) {
|
||||
res.status(400).json({ error: `${field} must be ${max} characters or less` });
|
||||
return;
|
||||
}
|
||||
next();
|
||||
};
|
||||
}
|
||||
|
||||
function validateStringLengths(maxLengths: Record<string, number>) {
|
||||
return (req: Request, res: Response, next: NextFunction): void => {
|
||||
for (const [field, max] of Object.entries(maxLengths)) {
|
||||
const value = req.body[field];
|
||||
if (value && typeof value === 'string' && value.length > max) {
|
||||
res.status(400).json({ error: `${field} must be ${max} characters or less` });
|
||||
return;
|
||||
}
|
||||
}
|
||||
next();
|
||||
};
|
||||
}
|
||||
|
||||
export { maxLength, validateStringLengths };
|
||||
Reference in New Issue
Block a user