utils/validateRequest.js – ভ্যালিডেশন হ্যান্ডলিং
আমরা একটি validateRequest ইউটিলিটি ফাংশন তৈরি করবো যা রিকোয়েস্টের ভ্যালিডেশন চেক করবে।
const ApiError = require("../utils/ApiError.js");
const HTTP_STATUS_CODES = require("../constants/httpStatus.js");
const validateRequest = (bodySchema, paramsSchema) => (req, res, next) => {
// Validate request body
if (bodySchema) {
const { error } = bodySchema.validate(req.body);
if (error) {
return next(new ApiError(HTTP_STATUS_CODES.BAD_REQUEST, error.details[0].message));
}
}
// Validate request parameters (e.g., req.params.id)
if (paramsSchema) {
const { error } = paramsSchema.validate(req.params);
if (error) {
return next(new ApiError(HTTP_STATUS_CODES.BAD_REQUEST, error.details[0].message));
}
}
next();
};
module.exports = validateRequest;