add chnanges
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { pick } from '../utils/handler/pick.handler';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import * as Yup from 'yup';
|
||||
import { ObjectSchema, ValidationError } from 'yup';
|
||||
import ApiError from '../utils/helper/ApiError';
|
||||
import { pick } from '../utils/handler/pick.handler';
|
||||
|
||||
/**
|
||||
* Validation middleware for Express routes.
|
||||
@@ -9,7 +9,7 @@ import ApiError from '../utils/helper/ApiError';
|
||||
* @returns Middleware function to validate request properties.
|
||||
*/
|
||||
const validate =
|
||||
(schema: Partial<Record<keyof Request, Yup.ObjectSchema<any>>>) =>
|
||||
(schema: Partial<Record<keyof Request, ObjectSchema<never>>>) =>
|
||||
(req: Request, res: Response, next: NextFunction): void => {
|
||||
// Define valid request keys explicitly
|
||||
const validRequestKeys = ['params', 'query', 'body', 'file', 'files'] as (keyof Request)[];
|
||||
@@ -31,11 +31,12 @@ const validate =
|
||||
// Assign validated values back to the request object
|
||||
validatedValues.forEach((value, index) => {
|
||||
const key = Object.keys(validSchema)[index];
|
||||
(req as any)[key] = value; // Type assertion since req is mutable
|
||||
// Safely assign the validated value to the request object
|
||||
req[key as keyof Request] = value; // Use `Request` here instead of `Request.ResBody`
|
||||
});
|
||||
next();
|
||||
})
|
||||
.catch((err: Yup.ValidationError) => {
|
||||
.catch((err: ValidationError) => {
|
||||
// Collect and format error messages
|
||||
const errorMessage = err.inner.map((detail) => detail.message).join(', ');
|
||||
next(new ApiError(400, errorMessage));
|
||||
|
||||
Reference in New Issue
Block a user