mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-22 06:41:46 +00:00
27 lines
891 B
TypeScript
27 lines
891 B
TypeScript
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 };
|