Wednesday, 26 April 2017

express-validator with mongoose

I'm developing my own website using (angularjs, bootstrap) for front end (node , express) for back end (mongodb , mongoose layer) for the database. I have a registration form and in that form I want to check that the email hasn't already been taken when someone try to create new account , so I want my registration api to check if the submitted email hasn't already been taken before.

I'm now using this validator https://github.com/ctavan/express-validator#validation-by-schema and this is my code :

var vadlidateRegUser= function (req,res,next) {
req.checkQuery('UserName','Username must contain letters and numbers only').isAlphanumeric().notEmpty();

req.checkQuery('Email','Email should have a valid syntax e.g: 
example@example.com').isEmail().notEmpty();

//I want something like this :
//req.checkQuery('Email','Email should have a valid syntax e.g: 
//example@example.com').isEmail().isUnique(Model Name);

var error = req.validationErrors();
if(!error){
next();
}else {
  res.json({success:false, message:error});
}

}



via Mahmoud Abd AL Kareem

No comments:

Post a Comment