0

使用类验证器,验证管道我想根据需要标记一些字段。我尝试使用@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;
}
// 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 UnhandledExceptionInterceptor());

    app.useGlobalFilters(new HttpErrorsExceptionFilter());

    app.useGlobalFilters(new UnhandledExceptionFilter(newLogger('UnhandledExceptionFilter')));

    app.useGlobalPipes(new ValidationPipe({skipMissingProperties: true}));

    app.useGlobalPipes(new ValidationPipe({forbidNonWhitelisted :true, whitelist:true, transform:true}));

}

样本输入:两个字段的样本输入。

{  
  "shippingAddress": {
    "address1":,
    "address2": null 
  }
}

在这种情况下,这提供了预期的 400,但是当输入如下所示时,我也需要一个错误,缺少一个必填字段,

{  
  "shippingAddress": {
    "address2": null
   } 
}
4

1 回答 1

0

class-validator只要您不添加@IsOptional()装饰器,这是标准功能。我将管道直接添加到控制器,但想法是一样的。请参阅下面的课程和标注:

// test-class.ts
import { IsString, IsOptional } from 'class-validator';

export class TestClass {
  @IsString()
  address1: string;

  @IsString()
  @IsOptional()
  address2?: string;
}
//test-valid.controller.ts
import { Controller, Post, Body, UsePipes, ValidationPipe } from '@nestjs/common';
import { TestClass } from './models/test-class';

@Controller('test-valid')
export class TestValidController {

  @Post()
  @UsePipes(new ValidationPipe({skipMissingProperties: true}))
  @UsePipes(new ValidationPipe({forbidNonWhitelisted: true, whitelist: true, transform: true}))
  async testValidate(@Body() body: TestClass) {
    return 'Valid!';
  }
}

# cURL callouts 
~/Documents/gitRepos/nest-testing
$ curl -X POST http://localhost:3000/test-valid \
> --header "Content-Type: application/json" \
> --data '{"address1": "some address", "address2": "some other address"}'
Valid!

~/Documents/gitRepos/nest-testing
$ curl -X POST http://localhost:3000/test-valid --header "Content-Type: a
pplication/json" --data '{"address2": "some other address"}'
[{"target":{"address2":"some other address"},"property":"address1","children":[],"constraints":{"isString":"address1 must be a string"}}]

~/Documents/gitRepos/nest-testing
$ curl -X POST http://localhost:3000/test-valid --header "Content-Type: a
pplication/json" --data '{"address1": "some address"}'
Valid!

你可以看看如果address1缺少class-validator会抛出一个错误。

查看您的代码,您的代码就在您的required: true上方address1: string,而不是在装饰器中,这可能会导致将装饰器应用于字段required而不是address1,这只是我想指出的。

于 2019-07-23T16:52:09.130 回答