Spaces:
Sleeping

File size: 556 Bytes
4e83772
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { COMMON_ERRORS } from "@/constants/errors.js";
import AppError from "@/utils/AppError.js";
import { STATUS_CODES } from "@/utils/statusCodes.js";
import { NextFunction, Request, Response } from "express";
import mongoose from "mongoose";

export const validateObjectId = (parameter: string) => {
	return (req: Request, res: Response, next: NextFunction) => {
		const id = req.params[parameter];
		if (!mongoose.Types.ObjectId.isValid(id)) {
			return next(new AppError(COMMON_ERRORS.INVALID_ID(id), STATUS_CODES.BAD_REQUEST));
		}
		next();
	};
};