使用类验证器,验证管道我想根据需要标记一些字段。我尝试使用@IsNotEmpty方法。当输入为空时,它会抛出 400 错误。但如果输入也丢失,我需要抛出错误。
DTO:地址对象,包含字段地址 1 和地址 2。我希望地址 1 为必需,地址 2 为可选
import {IsString, IsInt, IsNotEmpty } from 'class-validator';
import {ApiModelProperty} from '@nestjs/swagger';
export class Address {
@ApiModelProperty({description: 'Address Line 1', required : true})
@IsString()
@IsNotEmpty()
required : true
address1: string;
@ApiModelProperty({description: 'Address Line 2', required :false})
@IsString()
address2?: string;
}
Run Code Online (Sandbox Code Playgroud)
// App.js: Application file where validation pipes are defined.
async function bootstrap() {
const expressServer = express();
const app = await NestFactory.create(AppModule, expressServer, {bodyParser: true});
app.use(bodyParser.json({limit: 6851000}));
app.useGlobalInterceptors(new …Run Code Online (Sandbox Code Playgroud)