Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading