【发布时间】:2019-10-30 21:56:49
【问题描述】:
我正在使用管道进行请求验证。如果请求失败,我想重定向页面但不想抛出错误。问题是如何在验证中访问响应对象。
这是我的验证管道。
@Injectable()
export class ValidationPipe implements PipeTransform<any> {
async transform(value: any, { metatype }: ArgumentMetadata) {
if (!metatype || !this.toValidate(metatype)) {
return value;
}
const object = plainToClass(metatype, value);
const errors = await validate(object);
if (errors.length > 0) {
// in here i need to response with res.redirect('') function
throw new BadRequestException('Validation failed');
}
return value;
}
private toValidate(metatype: Function): boolean {
const types: Function[] = [String, Boolean, Number, Array, Object];
return !types.includes(metatype);
}
}
我需要访问 res.redirect() 函数而不是抛出异常
【问题讨论】:
标签: node.js typescript nestjs