【问题标题】:hapijs joi email validation option tldWhitelist usagehapijs joi 电子邮件验证选项 tldWhitelist 用法
【发布时间】:2016-02-04 05:58:50
【问题描述】:

我正在尝试使用 Joi 和 Hapijs 来验证 POST 提交请求。

这是我的招摇代码:

  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

【问题讨论】:

    标签: node.js validation email hapijs joi


    【解决方案1】:

    tldWhitelist 的值可以是对象查找表或有效顶级域的数组。例如:

    tldWhitelist: ['com']
    

    【讨论】:

    • 谢谢。如何验证提交的电子邮件是否来自 example.com?
    • 您可以使用正则表达式:Joi.string().email().regex(/example\.com$/)
    猜你喜欢
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    • 2016-05-09
    • 2016-05-08
    相关资源
    最近更新 更多