Looking for some advice on a current issue I'm facing. I have a userPassword value object in my user domain model and want to validate two cases when creating it:
Ensure the password is not empty
Hash the given value
I'm unsure of the best approach to achieve this. Here's the code snippet showcasing the problem:
export class UserPassword {
private readonly value:string
private constructor(value: string) {
this.value = value
}
public getValue():string {
return this.value
}
// Static factory method where validation should be applied
static create(password: string):UserPassword {
// Implementation needed here
}
private async hashPassword(password: string):Promise<string>{
return await bcrypt.hash(password,10)
}
async comparePassword(password:string):Promise<boolean> {
let hashed: string = this.value
return await bcrypt.compare(password, hashed)
}
}