class-validator

Nestjs

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;
}
05-11 13:03