I want to access the name property value into the password property while doing validation. Can you help me out? I tried this code but obviously name was not defined in the scope, hence was getting an error. I want to prevent the user from entering their name in the password section...
const userSchema = new mongoose.Schema({
name:{
type:String,
required:true,
trim:true,
lowercase:true
},
email:{
type:String,
required:true,
trim:true,
lowercase:true,
validate(value){
if(!validator.isEmail(value))
{
throw new Error('Email not valid!')
}
}
},
password:{
type:String,
validate(value){
if(value.length<6||value.length>16)
{
throw new Error('Password should be of lenght ranging 6 to 16 characters')
}
**if(validator.contains(value,*name*))
{
throw new Error('Password should not contain your Name in it')
}**
}
},
date:{
type:Date,
default:Date.now()
}
})
const User = mongoose.model('users',userSchema)
You can access your document using this
. So the name can be accessed by this.name
...
validate(value) {
if (value.length < 6 || value.length > 16) {
throw new Error('Password should be of length ranging 6 to 16 characters')
}
if (validator.contains(value, this.name)) {
throw new Error('Password should not contain your Name in it')
}
}
...