Browse Source

chore: add dto types and message for the milvus controller (#745)

Signed-off-by: ryjiang <jiangruiyi@gmail.com>
ryjiang 5 months ago
parent
commit
c58f215943

+ 7 - 7
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

+ 33 - 9
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[];
 }

+ 16 - 21
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 {