nest笔记七:参数校验
nestjs的参数校验官方文档:https://docs.nestjs.com/techniques/validation。 它主要使用第三方的class-validator来进行参数校验。
Validator-Pipe实现
- 网上给了很多例子,基本上可以直接拿来使用了,下面的这个是我做了一下处理的
import {
Injectable, PipeTransform, ArgumentMetadata, ValidationError, HttpException, HttpStatus } from '@nestjs/common';
import {
plainToClass } from 'class-transformer';
import {
validate } from 'class-validator';
/**
* 这是一个全局的参数验证管道,基于class-transformer
* 如果失败,则会抛出HttpException
* 在main.ts的nestApp要将它设为全局的
*/
@Injectable()
export class ValidationPipe implements PipeTransform {
async transform(value: any, {
metatype }: ArgumentMetadata) {
if (!metatype || !this.toValidate(metatype)) {
return value;
}
const object = plainToClass(metatype, value);
const errors = await validate(object);
const errorList: string[] = [];
const errObjList: ValidationError[] = [...errors];
do {
const e = errObjList.shift();
if (!e) {
break;
}
if (e.constraints) {
for (const item in e.constraints) {
errorList.push(e.constraints[item]);
}
}
if (e.children) {
errObjList.push(...e.children);
}
} while (true);
if (errorList.length > 0) {
throw new HttpException('请求参数校验错误:' + errorList.join(), HttpStatus.INTERNAL_SERVER_ERROR);
}
return object;
}
private toValidate(metatype: Function): boolean {
const types: Function[] = [String, Boolean, Number, Array, Object];
return !types.includes(metatype);
}
}
- 然后在app初始化,设置全局的pipe
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
logger: new