Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
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();

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

}

Now I would like to check with the validator if email is unique, something like this:

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

any suggestions ?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

express-validator can check and sanitize your input data but can't tell if an email is already in your database.

Best way I think is to make it unique in your mongoose Schema and handle error when email already exist.:

let User = new Schema({
    firstname: String,
    lastname: String,
    email: {
        type: String,
        required: true,
        unique: true,  // ensure unique email
        validate: [ isEmail, "Email should have a valid syntax e.g: example@example.com" ]
    },
    // ...
})
over 4 years ago · Santiago Trujillo Report

0

I know this question is old but i found it today hoping to help me with the same problem but I managed to find a way to do it, so I wanted to share it for the next one to find it.

Now express-validator has the ability to check with Promises.

What I did was to create a seperate file with my mongoose schema and require it in my router, then during my checks i did this:

check('email').custom((value) => {

    var query = User.find({ email: value})
    return query.exec().then(user => {
        if (user.length > 0) {
             return Promise.reject('E-mail already in use');
        }
    });
}),

and everything works like a charm!

I hope this helps someone else. If you need more specifics, just reply so I can add more detail.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!