【发布时间】:2020-07-30 09:04:54
【问题描述】:
我是 NEST JS 的新手,现在我尝试在 DTO'S 中包含一些验证器 看起来像:
// /blog-backend/src/blog/dto/create-post.dto.ts
import { IsEmail, IsNotEmpty, IsDefined } from 'class-validator';
export class CreatePostDTO {
@IsDefined()
@IsNotEmpty()
title: string;
@IsDefined()
@IsNotEmpty()
description: string;
@IsDefined()
@IsNotEmpty()
body: string;
@IsEmail()
@IsNotEmpty()
author: string;
@IsDefined()
@IsNotEmpty()
datePosted: string;
}
但是当我执行 post 服务时:
{
"title":"juanita"
}
它的回报很好! 但是验证器应该正确显示和错误吗?
我的帖子管理员
@Post('/post')
async addPost(@Res() res, @Body() createPostDTO: CreatePostDTO) {
console.log(createPostDTO)
const newPost = await this.blogService.addPost(createPostDTO);
return res.status(HttpStatus.OK).json({
message: 'Post has been submitted successfully!',
post: newPost,
});
}
我的 main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(5000);
}
bootstrap();
【问题讨论】:
标签: node.js typescript nestjs