我正在尝试将 Prisma 与 NestJS 提供的 ValidationPipe 一起使用,但它不起作用,我正在使用class-validator
带有 DTO(类)的包ValidationPipes
,它工作正常,现在我需要一种方法来使用与 Prisma 相同的模式而无需DTO 没有重复的类型。(我想避免创建自定义管道进行验证)
DTO 文件:
import { IsNotEmpty } from 'class-validator';
export class TodoCreateDto {
@IsNotEmpty()
title: string;
@IsNotEmpty()
description: string;
}
使用 DTO:工作
@Controller('todos')
export class TodosController {
constructor(private todosService: TodosService) {}
@Post()
@UsePipes(ValidationPipe)
createTodo(@Body() todoCreateDto: TodoCreateDto) {
return this.todosService.createTodo(todoCreateDto);
}
}
使用 PRISMA:不工作
@Controller('todos')
export class TodosController {
constructor(private todosService: TodosService) {}
@Post()
@UsePipes(ValidationPipe)
createTodo(@Body() todoCreateInput: Prisma.TodoCreateInput) {
return this.todosService.createTodo(todoCreateInput);
}
}