hapijs joi 电子邮件验证选项 tldWhitelist 用法
hapijs joi email validation option tldWhitelist usage
我正在尝试使用 Joi 和 Hapijs 来验证 POST 提交请求。
这是我的 swagger 代码:
server.route([
{
method: "POST",
path: '/cardsignup',
config: {
handler: card_signup,
description: 'Insert card signup record',
notes: 'save card signup to database',
tags: ['card', 'signup', 'api'],
plugins: {
'hapi-swagger': {
responseMessages: [
{code: 200, message: 'Success'},
{code: 400, message: 'Bad Request'},
{code: 401, message: 'Not Authorized'},
{code: 500, message: 'Internal Server Error'}
]
}
},
validate: {
payload: {
name: Joi.string()
.required()
.description('full name'),
email: Joi.string().email({
tldWhitelist:['example.com']
})
.required()
.description('email'),
signature_svg: Joi.string()
.optional()
.description('string encoded svg'),
signature_png: Joi.binary()
.optional()
.description('base64 encoded png'),
card_choice: Joi.string()
.required()
.description('card choice string'),
last4: Joi.number()
.required()
.description('last 4 digits of cc')
}
}
}
}
]);
当我对服务器执行 POST 时,无论我输入什么电子邮件,我都会得到这个:
{
"statusCode": 400,
"error": "Bad Request",
"message": "child \"email\" fails because [\"email\" must be a valid email]",
"validation": {
"source": "payload",
"keys": [
"email"
]
}
}
有人有如何使用 tldWhitelist 的示例吗?
具体来说,我想验证电子邮件是否来自 example.com
tldWhitelist
的值可以是对象查找 table 或有效顶级域的数组。例如:
tldWhitelist: ['com']
我正在尝试使用 Joi 和 Hapijs 来验证 POST 提交请求。
这是我的 swagger 代码:
server.route([
{
method: "POST",
path: '/cardsignup',
config: {
handler: card_signup,
description: 'Insert card signup record',
notes: 'save card signup to database',
tags: ['card', 'signup', 'api'],
plugins: {
'hapi-swagger': {
responseMessages: [
{code: 200, message: 'Success'},
{code: 400, message: 'Bad Request'},
{code: 401, message: 'Not Authorized'},
{code: 500, message: 'Internal Server Error'}
]
}
},
validate: {
payload: {
name: Joi.string()
.required()
.description('full name'),
email: Joi.string().email({
tldWhitelist:['example.com']
})
.required()
.description('email'),
signature_svg: Joi.string()
.optional()
.description('string encoded svg'),
signature_png: Joi.binary()
.optional()
.description('base64 encoded png'),
card_choice: Joi.string()
.required()
.description('card choice string'),
last4: Joi.number()
.required()
.description('last 4 digits of cc')
}
}
}
}
]);
当我对服务器执行 POST 时,无论我输入什么电子邮件,我都会得到这个:
{
"statusCode": 400,
"error": "Bad Request",
"message": "child \"email\" fails because [\"email\" must be a valid email]",
"validation": {
"source": "payload",
"keys": [
"email"
]
}
}
有人有如何使用 tldWhitelist 的示例吗?
具体来说,我想验证电子邮件是否来自 example.com
tldWhitelist
的值可以是对象查找 table 或有效顶级域的数组。例如:
tldWhitelist: ['com']