Nestjs 接口验证

class-validator

Nestjs

yarn add class-transformer class-validator

main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common' async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe({
disableErrorMessages: true, // 不显示错误信息
whitelist: true, // 开启过滤
}))
await app.listen(5000);
}
bootstrap();

controller

import { signInDto } from './dto/signin.dto'

  @Post('signin')
signIn(@Body() body: signInDto ){
l(body)
return this.appService.signIn(body)
}

signin.dto.ts

import { IsNotEmpty, Length, IsString } from 'class-validator'
export class signInDto { @IsNotEmpty()
@IsString()
username: string; @IsNotEmpty({
message: '密码不能为空?'
})
@Length(6, 12)
password: string;
}
上一篇:cocos2d-x 使用UIWebView加载网页(顺便可以看到如何用OC调C++)


下一篇:epoll 系列函数简介、与select、poll 的区别