From 918b6234068d30200fa6e2c9e1cf81c875068bcc Mon Sep 17 00:00:00 2001 From: ryjiang Date: Tue, 21 Jan 2025 10:33:30 +0800 Subject: [PATCH] chore: add dto types and message for the milvus controller Signed-off-by: ryjiang --- server/src/middleware/validation.ts | 14 ++++----- server/src/milvus/dto.ts | 42 ++++++++++++++++++++------ server/src/milvus/milvus.controller.ts | 37 ++++++++++------------- 3 files changed, 56 insertions(+), 37 deletions(-) diff --git a/server/src/middleware/validation.ts b/server/src/middleware/validation.ts index fecc85fe..98c4ae05 100644 --- a/server/src/middleware/validation.ts +++ b/server/src/middleware/validation.ts @@ -1,8 +1,8 @@ -import { RequestHandler } from "express"; -import { plainToClass } from "class-transformer"; -import { validate, ValidationError } from "class-validator"; -import { sanitize } from "class-sanitizer"; -import HttpException from "../exception/HttpException"; +import { RequestHandler } from 'express'; +import { plainToInstance } from 'class-transformer'; +import { validate, ValidationError } from 'class-validator'; +import { sanitize } from 'class-sanitizer'; +import HttpException from '../exception/HttpException'; /** * Only check for req.body @@ -17,7 +17,7 @@ export const dtoValidationMiddleware = ( skipMissingProperties = false ): RequestHandler => { return (req, res, next) => { - const dtoObj = plainToClass(type, req.body); + const dtoObj = plainToInstance(type, req.body); validate(dtoObj, { skipMissingProperties }).then( (errors: ValidationError[]) => { if (errors.length > 0) { @@ -25,7 +25,7 @@ export const dtoValidationMiddleware = ( .map((error: ValidationError) => (Object as any).values(error.constraints) ) - .join(", "); + .join(', '); next(new HttpException(400, dtoErrors)); } else { // sanitize the object and call the next middleware diff --git a/server/src/milvus/dto.ts b/server/src/milvus/dto.ts index e82730a2..2dbee3eb 100644 --- a/server/src/milvus/dto.ts +++ b/server/src/milvus/dto.ts @@ -1,25 +1,49 @@ -import { ArrayMinSize, IsArray, IsOptional, IsString } from "class-validator"; +import { + ArrayMinSize, + IsArray, + IsOptional, + IsString, + IsNotEmpty, + IsBoolean, +} from 'class-validator'; export class ConnectMilvusDto { - @IsString() + @IsString({ message: 'address must be a string.' }) + @IsNotEmpty({ message: 'address is required.' }) readonly address: string; + @IsString({ message: 'database must be a string.' }) @IsOptional() readonly database: string; -} -export class CheckMilvusDto { - @IsString() - readonly address: string; + @IsString({ message: 'username must be a string.' }) + readonly username: string; + + @IsString({ message: 'password must be a string.' }) + readonly password: string; + + @IsString({ message: 'token must be a string.' }) + @IsOptional() + readonly token: string; + + @IsBoolean({ message: 'checkHealth must be a boolean.' }) + @IsOptional() + readonly checkHealth: boolean; + + @IsOptional({ message: 'clientId is optional.' }) + readonly clientId: string; } export class UseDatabaseDto { - @IsString() + @IsString({ message: 'database name must be a string.' }) + @IsNotEmpty({ message: 'database name is required.' }) readonly database: string; } export class FlushDto { - @IsArray() - @ArrayMinSize(1, { message: "At least need one collection name." }) + @IsArray({ message: 'collection_names must be an array of strings.' }) + @ArrayMinSize(1, { + message: 'collection_names must contains at least 1 item.', + }) readonly collection_names: string[]; } diff --git a/server/src/milvus/milvus.controller.ts b/server/src/milvus/milvus.controller.ts index 8e2ee9fa..a6b0c4ed 100644 --- a/server/src/milvus/milvus.controller.ts +++ b/server/src/milvus/milvus.controller.ts @@ -40,26 +40,13 @@ export class MilvusController { return this.router; } - async connectMilvus(req: Request, res: Response, next: NextFunction) { - const { - address, - username, - password, - database, - token, - checkHealth, - clientId, - } = req.body; + async connectMilvus( + req: Request<{}, {}, ConnectMilvusDto>, + res: Response, + next: NextFunction + ) { try { - const result = await this.milvusService.connectMilvus({ - address, - token, - username, - password, - database, - checkHealth, - clientId, - }); + const result = await this.milvusService.connectMilvus(req.body); res.send(result); } catch (error) { @@ -67,7 +54,11 @@ export class MilvusController { } } - async flush(req: Request, res: Response, next: NextFunction) { + async flush( + req: Request<{}, {}, FlushDto>, + res: Response, + next: NextFunction + ) { const collectionNames = req.body; try { const result = await this.milvusService.flush( @@ -97,7 +88,11 @@ export class MilvusController { res.send(data); } - async useDatabase(req: Request, res: Response, next: NextFunction) { + async useDatabase( + req: Request<{}, {}, UseDatabaseDto>, + res: Response, + next: NextFunction + ) { const { database } = req.body; try {