Validator.js: Cant validate email using mongoose

Created on 26 Feb 2016  路  5Comments  路  Source: validatorjs/validator.js

I have a pretty simple validation in a mongoose schema, and it used to work perfectly. But for some weird reason, after updating to 5.0.0 it doesn't work anymore...

When I use version 5.0.0 the validation process stuck, like it was in an infinite loop. But if I go back to version 4.9, it works.

I tryied to debug, but I couldnt find the problem. Aparently isEmail funcition returns normally.

email: {
    type: String,
    index: { unique: true },
    required: true,
    validate: [validator.isEmail, 'user.invalidemail']
},

https://github.com/rodrigogs/nodejs-web-jade-scaffold

Most helpful comment

Another possibility (synchronous instead of asynchronous):

validate: {
  validator: email => validator.isEmail(email),
  message: '{VALUE} is not a valid email'
}

All 5 comments

You'll have to open a ticket over at the mongoose repo. As you said, the isEmail validator is returning normally when you use this library directly.

But it stoped working due to some change made in validator module. Otherwise, returning to 4.9 would not solve the problem.

The issue is not with the isEmail function but with the way that mongoose calls it. It's not my responsibility to fix bugs in the libraries that depend on this one.

For anyone who comes across this, make sure you check out the Mongoose docs, this can be solved simply:

email: {
    type: String,
    unique: true,
    lowercase: true,
    required: true,
    validate: {
      isAsync: true,
      validator: (v, cb) => {
        cb(isEmail(v));
      },
      message: '{VALUE} is not a valid email'
    }
  },

http://mongoosejs.com/docs/validation.html#async-custom-validators

Another possibility (synchronous instead of asynchronous):

validate: {
  validator: email => validator.isEmail(email),
  message: '{VALUE} is not a valid email'
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

IOAyman picture IOAyman  路  4Comments

woverton picture woverton  路  4Comments

jaxkodex picture jaxkodex  路  3Comments

philfreo picture philfreo  路  3Comments

AnandChowdhary picture AnandChowdhary  路  3Comments