我现在有些问题,我想知道你的意见。首先,我有一个userPassword,它表示用户域模型中的值对象。在创建我的userPassword对象时,我想验证2种情况:
的空值
因此,
导出类UserPassword {私有只读值:字符串私有构造函数(值: this.value = value }公共getValue():string {返回this.value } // My静态工厂,其中我希望应用验证情况静态创建(密码:字符串):UserPassword{ //需要实现}私有异步hashPassword(密码: string):承诺{返回等待bcrypt.hash(密码),)}异步comparePassword(密码:字符串):承诺{ let散列: string = this.value返回等待bcrypt.compare(密码,散列)}
}
发布于 2021-11-14 22:05:34
为了验证目的,我只是在创建密码对象之前做了一些验证。因为我认为它总是与对象构造中定义的验证逻辑相关。所以我的解决方案是:
export class UserPassword {
private readonly value:string
constructor(value: string) {
if (value === "") {
throw new UserInputError("Password must be filled")
}
else {
this.hashPassword(value).then(r => console.log(r))
this.value = value
}
}
public getValue():string {
return this.value
}
private async hashPassword(password: string):Promise<string>{
return bcrypt.hash(password,10)
}
async comparePassword(password:string):Promise<boolean> {
let hashed: string = this.value
return bcrypt.compare(password, hashed)
}
}发布于 2021-11-15 22:08:01
像这样吗?我的意思是,如果你想确定,不要在内存中存储密码的清晰值。
// My static factory in which i want to apply my validations cases
static async create(password: string):UserPassword {
if (!password || password.trim().length() == 0) {
throw Exception("password not valid")
}
var hash = await bcrypt.hash(password,10)
return new UserPassword(hash)
}https://stackoverflow.com/questions/69966787
复制相似问题