ark*_*rke 2 class-validator nestjs
使用类验证器,验证管道我想根据需要标记一些字段。我尝试使用@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 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}));
}
Run Code Online (Sandbox Code Playgroud)
示例输入:包含两个字段的示例输入。
{
"shippingAddress": {
"address1":,
"address2": null
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,这会按预期提供 400,但是当输入如下所示缺少必填字段之一时,我还需要一个错误,
{
"shippingAddress": {
"address2": null
}
}
Run Code Online (Sandbox Code Playgroud)
class-validator只要您不添加装饰器,这就是标准功能@IsOptional()。我将管道直接添加到控制器,但想法是相同的。请参阅下面的我的课程和标注:
// test-class.ts
import { IsString, IsOptional } from 'class-validator';
export class TestClass {
@IsString()
address1: string;
@IsString()
@IsOptional()
address2?: string;
}
Run Code Online (Sandbox Code Playgroud)
//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!';
}
}
Run Code Online (Sandbox Code Playgroud)
# 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!
Run Code Online (Sandbox Code Playgroud)
可以看到如果address1缺少class-validator就会抛出错误。
查看您的代码,您的代码位于required: true您的正上方address1: string,而不是在装饰器中,这可能会导致装饰器应用于字段required而不是的问题address1,这只是我想指出的。
| 归档时间: |
|
| 查看次数: |
21093 次 |
| 最近记录: |