Skip to content

Commit

Permalink
chore: add dto types and message for the milvus controller
Browse files Browse the repository at this point in the history
Signed-off-by: ryjiang <[email protected]>
  • Loading branch information
shanghaikid committed Jan 21, 2025
1 parent ff1c19b commit 918b623
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 37 deletions.
14 changes: 7 additions & 7 deletions server/src/middleware/validation.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,15 +17,15 @@ 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) {
const dtoErrors = errors
.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
Expand Down
42 changes: 33 additions & 9 deletions server/src/milvus/dto.ts
Original file line number Diff line number Diff line change
@@ -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[];
}
37 changes: 16 additions & 21 deletions server/src/milvus/milvus.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,25 @@ 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) {
next(error);
}
}

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(
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 918b623

Please sign in to comment.