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

无人久伴 提交于 2021-01-27 05:19:10

问题


I'm using mongoose 4.9.0. Whilst the code below works, I get a warning like the following:

(node:24769) DeprecationWarning: Implicit async custom 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 info.

I suspect the error is coming from the 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 I seem to have is isEmail from the validator library, which given an string value returns whether it is valid or not.


回答1:


There's a sneaky mistake in your custom validator.

isEmail function from the validator library takes 2 parameters, even though the second one is completely optional.

isEmail(str [, options])

You can prevent mongoose from seeing it by creating a function on top of it:

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



回答2:


Based on the mongoose Documentation you can remove the warning by setting the isAsync attribute of the validator to false. Mongoose assumes that all functions that take in two parameters are asynchronous

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

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




回答3:


//add validator to user model schema 

var validator = require('validator');

validate:{
       validator: (value)=>{

         return validator.isEmail(value);  

       },

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

  },



回答4:


Just make copy for the below codes and will work without any errors

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' 
     }
  }



回答5:


PROBLEM

import validate from 'mongoose-validator'

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

SOLUTION

import validate from 'mongoose-validator'

const isEmail = validate.validatorjs.isEmail

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



回答6:


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


来源:https://stackoverflow.com/questions/42877930/implicit-async-custom-validators-custom-validators-that-take-2-arguments-are-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!