fix : sep gateway
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
import { ClassConstructor, plainToInstance } from "class-transformer";
|
||||
import { ValidationError, validate } from "class-validator";
|
||||
import { NextFunction, Request, RequestHandler, Response } from "express";
|
||||
import { injectable } from "inversify";
|
||||
|
||||
import { BadRequestError } from "../app/app.errors";
|
||||
|
||||
@injectable()
|
||||
export class ValidationMiddleware {
|
||||
static validateInputWithLogging(DTO: ClassConstructor<any>, logPrefix: string = "Request Body"): RequestHandler {
|
||||
return async (req: Request, _res: Response, next: NextFunction): Promise<void> => {
|
||||
try {
|
||||
// Log the raw request body before validation
|
||||
console.log(`${logPrefix}:`, JSON.stringify(req.body, null, 2));
|
||||
|
||||
// Convert the request body to an instance of the DTO class
|
||||
const validationClass = plainToInstance(DTO, req.body, {
|
||||
excludeExtraneousValues: true,
|
||||
});
|
||||
|
||||
// Validate the DTO instance, including nested objects
|
||||
const errors: ValidationError[] = await validate(validationClass, {
|
||||
whitelist: true,
|
||||
// forbidNonWhitelisted: true,
|
||||
// transform: true,
|
||||
});
|
||||
if (errors.length > 0) {
|
||||
const errorMsg = errors.map((error) => ValidationMiddleware.flattenValidationErrors(error)).flat();
|
||||
|
||||
next(new BadRequestError(errorMsg));
|
||||
// throw new BadRequestError("Bad Request", errorMsg);
|
||||
} else {
|
||||
req.body = validationClass;
|
||||
next();
|
||||
}
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static validateInput(DTO: ClassConstructor<any>): RequestHandler {
|
||||
return async (req: Request, _res: Response, next: NextFunction): Promise<void> => {
|
||||
try {
|
||||
// Convert the request body to an instance of the DTO class
|
||||
const validationClass = plainToInstance(DTO, req.body, {
|
||||
excludeExtraneousValues: true,
|
||||
});
|
||||
|
||||
// Validate the DTO instance, including nested objects
|
||||
const errors: ValidationError[] = await validate(validationClass, {
|
||||
whitelist: true,
|
||||
// forbidNonWhitelisted: true,
|
||||
// transform: true,
|
||||
});
|
||||
if (errors.length > 0) {
|
||||
const errorMsg = errors.map((error) => ValidationMiddleware.flattenValidationErrors(error)).flat();
|
||||
|
||||
next(new BadRequestError(errorMsg));
|
||||
// throw new BadRequestError("Bad Request", errorMsg);
|
||||
} else {
|
||||
req.body = validationClass;
|
||||
next();
|
||||
}
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static validateParameter(DTO: ClassConstructor<any>): RequestHandler {
|
||||
return async (req: Request, _res: Response, next: NextFunction): Promise<void> => {
|
||||
try {
|
||||
// Convert the request body to an instance of the DTO class
|
||||
const validationClass = plainToInstance(DTO, req.params, {
|
||||
excludeExtraneousValues: true,
|
||||
enableImplicitConversion: true,
|
||||
});
|
||||
|
||||
// Validate the DTO instance, including nested objects
|
||||
const errors: ValidationError[] = await validate(validationClass, {
|
||||
whitelist: true,
|
||||
// forbidNonWhitelisted: true,
|
||||
// transform: true,
|
||||
});
|
||||
if (errors.length > 0) {
|
||||
const errorMsg = errors.map((error) => ValidationMiddleware.flattenValidationErrors(error)).flat();
|
||||
|
||||
next(new BadRequestError(errorMsg));
|
||||
// throw new BadRequestError("Bad Request", errorMsg);
|
||||
} else {
|
||||
req.query = validationClass;
|
||||
next();
|
||||
}
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static validateQuery(DTO: ClassConstructor<any>): RequestHandler {
|
||||
return async (req: Request, _res: Response, next: NextFunction): Promise<void> => {
|
||||
try {
|
||||
// Convert the request body to an instance of the DTO class
|
||||
const validationClass = plainToInstance(DTO, req.query, {
|
||||
excludeExtraneousValues: true,
|
||||
enableImplicitConversion: true,
|
||||
});
|
||||
|
||||
// Validate the DTO instance, including nested objects
|
||||
const errors: ValidationError[] = await validate(validationClass, {
|
||||
whitelist: true,
|
||||
// forbidNonWhitelisted: true,
|
||||
// transform: true,
|
||||
});
|
||||
if (errors.length > 0) {
|
||||
const errorMsg = errors.map((error) => ValidationMiddleware.flattenValidationErrors(error)).flat();
|
||||
|
||||
next(new BadRequestError(errorMsg));
|
||||
// throw new BadRequestError("Bad Request", errorMsg);
|
||||
} else {
|
||||
req.query = validationClass;
|
||||
next();
|
||||
}
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Helper function to recursively flatten nested validation errors
|
||||
private static flattenValidationErrors(error: ValidationError): string[] {
|
||||
if (error.constraints) {
|
||||
return Object.values(error.constraints);
|
||||
}
|
||||
|
||||
if (error.children && error.children.length > 0) {
|
||||
return error.children.map(ValidationMiddleware.flattenValidationErrors).flat();
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user