隐式异步自定义验证器(带有 2 个参数的自定义验证器)在 mongoose >= 4.9.0 中被弃用
Implicit async custom validators (custom validators that take 2 arguments) are deprecated in mongoose >= 4.9.0
我正在使用猫鼬 4.9.0。虽然下面的代码有效,但我收到如下警告:
(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.
我怀疑错误来自模型验证器。
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);
我似乎拥有的唯一自定义验证器是 validator
库中的 isEmail
,它给出了一个字符串值 returns 是否有效。
您的自定义验证器中存在一个偷偷摸摸的错误。
isEmail
function from the validator
库有两个参数,尽管第二个参数是完全可选的。
isEmail(str [, options])
您可以通过在其上创建一个函数来防止 mongoose 看到它:
validate: [{ validator: value => isEmail(value), msg: 'Invalid email.' }]
//add validator to user model schema
var validator = require('validator');
validate:{
validator: (value)=>{
return validator.isEmail(value);
},
message:'{VALUE} is not a valid Email'
},
只需复制以下代码即可正常运行
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'
}
}
问题
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'
}
}
[...]
根据 mongoose 文档,您可以通过将验证器的 isAsync 属性设置为 false 来消除警告。 Mongoose 假设所有接受两个参数的函数都是异步的
validate: [{ isAsync:false, validator: isEmail, msg: 'Invalid email.' }]
http://mongoosejs.com/docs/validation.html#async-custom-validators
var validator = require('validator');
validate: {
validator: (value) => validator.isEmail(value),
message: "{VALUE} is not a valid email"
}
我正在使用猫鼬 4.9.0。虽然下面的代码有效,但我收到如下警告:
(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.
我怀疑错误来自模型验证器。
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);
我似乎拥有的唯一自定义验证器是 validator
库中的 isEmail
,它给出了一个字符串值 returns 是否有效。
您的自定义验证器中存在一个偷偷摸摸的错误。
isEmail
function from the validator
库有两个参数,尽管第二个参数是完全可选的。
isEmail(str [, options])
您可以通过在其上创建一个函数来防止 mongoose 看到它:
validate: [{ validator: value => isEmail(value), msg: 'Invalid email.' }]
//add validator to user model schema
var validator = require('validator');
validate:{
validator: (value)=>{
return validator.isEmail(value);
},
message:'{VALUE} is not a valid Email'
},
只需复制以下代码即可正常运行
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'
}
}
问题
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'
}
}
[...]
根据 mongoose 文档,您可以通过将验证器的 isAsync 属性设置为 false 来消除警告。 Mongoose 假设所有接受两个参数的函数都是异步的
validate: [{ isAsync:false, validator: isEmail, msg: 'Invalid email.' }]
http://mongoosejs.com/docs/validation.html#async-custom-validators
var validator = require('validator');
validate: {
validator: (value) => validator.isEmail(value),
message: "{VALUE} is not a valid email"
}