Tam*_*mas 36 mongoose mongodb node.js
我有一个用户的mongoose模式(UserSchema),我想验证电子邮件是否具有正确的语法.我目前使用的验证如下:
UserSchema.path('email').validate(function (email) {
return email.length
}, 'The e-mail field cannot be empty.')
Run Code Online (Sandbox Code Playgroud)
但是,这仅检查字段是否为空,而不是语法.
是否已经存在我可以重复使用的东西,或者我必须提出自己的方法并在validate函数中调用它?
ram*_*n22 82
您还可以使用match或validate属性进行模式验证
例
var validateEmail = function(email) {
var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
return re.test(email)
};
var EmailSchema = new Schema({
email: {
type: String,
trim: true,
lowercase: true,
unique: true,
required: 'Email address is required',
validate: [validateEmail, 'Please fill a valid email address'],
match: [/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/, 'Please fill a valid email address']
}
});
Run Code Online (Sandbox Code Playgroud)
Kri*_*ekk 56
我使用验证器进行输入卫生,它可以很酷的方式使用.
安装它,然后像这样使用它:
import { isEmail } from 'validator';
// ...
const EmailSchema = new Schema({
email: {
//... other setup
validate: [ isEmail, 'invalid email' ]
}
});
Run Code Online (Sandbox Code Playgroud)
是一种享受,并且读得很好.
dan*_*p32 14
你可以使用正则表达式.看看这个问题:在JavaScript中验证电子邮件地址?
我过去曾经用过这个.
UserSchema.path('email').validate(function (email) {
var emailRegex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
return emailRegex.test(email.text); // Assuming email has a text attribute
}, 'The e-mail field cannot be empty.')
Run Code Online (Sandbox Code Playgroud)
小智 8
该验证程序 不到风度与猫鼬发挥出色,以摆脱警告的设置isAsync为假
const validator = require('validator');
email:{
type:String,
validate:{
validator: validator.isEmail,
message: '{VALUE} is not a valid email',
isAsync: false
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
我知道这很旧,但我没有看到这个解决方案,所以我想分享一下:
const schema = new mongoose.Schema({
email: {
type: String,
trim: true,
lowercase: true,
unique: true,
validate: {
validator: function(v) {
return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(v);
},
message: "Please enter a valid email"
},
required: [true, "Email required"]
}
});
Run Code Online (Sandbox Code Playgroud)
您可以对要验证的任何类型执行此操作,只需传递适当的正则表达式即可。如果您在 google 上搜索要验证的类型及其相关的正则表达式,则很容易找到解决方案。这将使您的验证保持一致,并将所有代码放在架构中而不是挂起函数。
由于某种原因,测试效果不佳validate: [ isEmail, 'Invalid email.']validate()。
const user = new User({ email: 'invalid' });
try {
const isValid = await user.validate();
} catch(error) {
expect(error.errors.email).to.exist; // ... it never gets to that point.
}
Run Code Online (Sandbox Code Playgroud)
但是mongoose 4.x(它也可能适用于旧版本)有其他与单元测试协同工作的替代选项。
单个验证器:
email: {
type: String,
validate: {
validator: function(value) {
return value === 'correct@example.com';
},
message: 'Invalid email.',
},
},
Run Code Online (Sandbox Code Playgroud)
多个验证器:
email: {
type: String,
validate: [
{ validator: function(value) { return value === 'handsome@example.com'; }, msg: 'Email is not handsome.' },
{ validator: function(value) { return value === 'awesome@example.com'; }, msg: 'Email is not awesome.' },
],
},
Run Code Online (Sandbox Code Playgroud)
如何验证电子邮件:
我的建议:将其留给投入数百小时构建适当验证工具的专家。(也已经在这里回答了)
npm install --save-dev validator
import { isEmail } from 'validator';
...
validate: { validator: isEmail , message: 'Invalid email.' }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
44356 次 |
| 最近记录: |