小编Pie*_*uso的帖子

使用`class-validator`在TypeScript中确认密码

今天,我试图弄清楚如何在应用程序的后端 (NestJS) 中验证注册表单。我只是想知道是否存在一种验证passwordpasswordConfirm匹配的方法,使用class-validator包来构建自定义验证器或利用提供的验证器。我在考虑类验证器,而不是字段验证器。

// Maybe validator here
export class SignUpDto {
    @IsString()
    @MinLength(4)
    @MaxLength(20)
    username: string;

    @IsString()
    @MinLength(4)
    @MaxLength(20)
    @Matches(/((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/, {message: 'password too weak'})
    password: string;

    @IsString()
    @MinLength(4)
    @MaxLength(20)
    passwordConfirm: string;
}
Run Code Online (Sandbox Code Playgroud)

你有什么建议?

typescript class-validator nestjs

16
推荐指数
4
解决办法
1万
查看次数

标签 统计

class-validator ×1

nestjs ×1

typescript ×1