Implicit asynchronous custom validators (custom validators that take 2 arguments) are deprecated in mongoose> = 4.9.0

I am using mongoose 4.9.0. While the code below works, I get a warning similar to the following:

(node: 24769) DeprecationWarning: implicit asynchronous validators (custom validators that take 2 arguments) are deprecated in mongoose> = 4.9.0. See http://mongoosejs.com/docs/validation.html#async-custom-validators for more details .

I suspect the error comes from model validators.

const mongoose = require('mongoose');
const isEmail = require('validator/lib/isEmail');

const Schema = mongoose.Schema;

const userSchema = new Schema({
  email: {
    type: String,
    unique: true,
    required: true,
    validate: [{ validator: isEmail, msg: 'Invalid email.' }],
  },
});

module.exports = mongoose.model('User', userSchema);

The only custom validator that seems to me to be isEmailfrom a library validatorthat gives a string value is whether it returns valid or not.

+4
5

.

isEmail validator 2 , .

isEmail(str [, options])

, :

validate: [{ validator: value => isEmail(value), msg: 'Invalid email.' }]
+9

Mongoose, , isAsync false. Mongoose , , ,

validate: [{ isAsync:false, validator: isEmail, msg: 'Invalid email.' }]

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

+4
//add validator to user model schema 

var validator = require('validator');

validate:{
       validator: (value)=>{

         return validator.isEmail(value);  

       },

       message:'{VALUE} is not a valid Email'

  },
+2

email: {
    type: String,
    required : true,
    lowercase : true,
    unique:true,
    validate: { 
    validator:function validateEmail(email) {
    var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
},   message: '{email} Invalid' 
     }
  }
0

import validate from 'mongoose-validator'

[...]
    email: {
      type: String,
      validate: validate({
        validator: 'isEmail',
        message: 'is not valid',
      })
    }
[...]

import validate from 'mongoose-validator'

const isEmail = validate.validatorjs.isEmail

[...]
    email: {
      type: String,
      validate: {
        validator: value => isEmail(value),
        message: 'is not valid'
      }
    }
[...]
0

Source: https://habr.com/ru/post/1672641/


All Articles