我正在创建一个逻辑来验证所有http body属性是否都是我的DTO的强制性组成部分。使用Nestjs中的自定义装饰器和拦截器,我可以访问exec上下文元数据中的请求body,但在验证时,我应该能够验证body属性是否是我的DTO属性的一部分-尽管最后那些属性我不能访问。
尽管这实际上是一个 typescript 的疑问,我告诉你的背景,因为如果你知道另一种方法来做我正在努力,请随时让我知道这种方式了。
下面的代码示例显示了我尝试访问的DTO属性:
export class CreateUserDTO {
@IsString()
@IsNotEmpty()
name: string;
@IsEmail()
email: string;
@MinLength(6)
@MaxLength(32)
@IsAlphanumeric()
password: string;
@IsOptional()
@IsDateString()
birthAt: string | Date;
}
// trying to do something like this, but can't access any property in transpilation time, one time they're all undefined
console.log(CreateUserDTO.prototype.birthAt);
console.log(CreateUserDTO.birthAt);
1条答案
按热度按时间ia2d9nvy1#
我可以通过在类验证器配置中将“whitelist”选项设置为true来解决这个问题,这个特性会去除已验证(返回)对象中不使用任何验证装饰器的任何属性。
参考:https://docs.nestjs.com/techniques/validation#using-the-built-in-validationpipe