'How to compare passwords in a mongoose schema?
How can I compare password and password_confirmation, and if they don't match then I'll print "Passwords don't match" to the error message?
const userSchema = new Schema(
{
username: {type: String, required: [true, "Please enter a username"], unique: [true, "Username taken"]},
password: {type: String, required: true, minLength: [8, "Minimum password lenth is 8"]},
password_confirmation: {type: String}
},
{ timestamps: true }
);
Solution 1:[1]
You can use validator in your schema. https://www.npmjs.com/package/validator
passwordConfirm: {
type: String,
required: [true, 'Please confirm your password'],
validate: {
// this only works on CREATE and SAVE!!!!
validator: function (el) {
return el === this.password;
},
message: 'Password and password confirmation do not match.',
},
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gesuchter |
