隐式异步自定义验证器(带有 2 个参数的自定义验证器)在 mongoose >= 4.9.0 中已弃用 [英] Implicit async custom validators (custom validators that take 2 arguments) are deprecated in mongoose >= 4.9.0

查看:41
本文介绍了隐式异步自定义验证器(带有 2 个参数的自定义验证器)在 mongoose >= 4.9.0 中已弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是猫鼬 4.9.0.虽然下面的代码有效,但我收到如下警告:

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

(node:24769) DeprecationWarning:隐式异步自定义验证器(带有 2 个参数的自定义验证器)在 mongoose >= 4.9.0 中已弃用.请参阅 http://mongoosejs.com/docs/validation.html#async-custom-验证器了解更多信息.

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

我似乎拥有的唯一自定义验证器是 中的 isEmail验证器库,给定一个字符串值返回它是否有效.

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.

推荐答案

您的自定义验证器中存在一个偷偷摸摸的错误.

There's a sneaky mistake in your custom validator.

isEmail 来自 validator 库接受 2 个参数,尽管第二个参数是完全可选的.

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 个参数的自定义验证器)在 mongoose >= 4.9.0 中已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆