express-validator: isEmail() 不会报告 body.email 不存在。

huangapple go评论67阅读模式
英文:

express-validator: isEmail() won't report if body.email doesn't exist

问题

更新:

express-validator 7.0.1,node 16.16.0

  1. https://express-validator.github.io/docs/guides/validation-chain 上按照第一个示例操作。
  2. 我的初始代码如下:
app.post('/newsletter',
  body("email")
    .isEmail()
    .withMessage('缺少电子邮件'),
// 其他代码...

当运行单元测试时,使用一个没有电子邮件字段的请求体时,会出现以下错误信息:
接收到:"将 varchar 值 'undefined' 转换为 int 数据类型时发生转换错误。"

为什么它不能检测到没有电子邮件?

  1. 然后,我将代码更改如下:
app.post('/newsletter',
  body("email")
    .exists()
    .withMessage('缺失')
    .isEmail()
    .withMessage('错误的电子邮件'),
// 其他代码...

它仍然显示与步骤2相同的错误消息。

请问正确的方法是什么,用于检测电子邮件字段是否缺失?


从7/11的更新,我已经成功让步骤3工作起来。
isEmail() 不能很好地处理所有情况,它会输出通用的 "无效值",例如没有电子邮件字段或非常长的电子邮件。这种通用的错误对最终用户没有帮助。

英文:

Updated:

express-validator 7.0.1, node 16.16.0

  1. follow the 1st example at https://express-validator.github.io/docs/guides/validation-chain
  2. my initial code is like this
app.post('/newsletter',
  body("email")
    .isEmail()
    .withMessage('missing email'),
....

when running unit test with a body without email field, it says
Received: "Conversion failed when converting the varchar value 'undefined' to data type int."

Why it cannot detect there's no email ?

  1. then i changed the code like this
    app.post('/newsletter',
      body("email")
        .exists()
        .withMessage('missing')
        .isEmail()
        .withMessage('wrong email'),
    ....

it still says the same error msg from step 2.

What's the correct way to detect the email field is missing pls ?


Update from 7/11, i was able to make step 3 to work.
isEmail() is not good enough to handle all cases, it outputs general "invalid value", e.g. if no email field or a very long email. Such general error is not helpful to end user.

答案1

得分: 1

我不知道你的完整代码,我不认为我能帮助你,但在我看来,你可以尝试这样做:

app.post('/newsletter',
  body("email")
    .exists({ checkFalsy: true })
    .withMessage('缺失')
    .isEmail()
    .withMessage('错误的电子邮件'),
  ....
英文:

I don't know your full code and I don't think I can help you, but in my opinion you can try this:

app.post('/newsletter',
  body("email")
    .exists({ checkFalsy: true })
    .withMessage('missing')
    .isEmail()
    .withMessage('wrong email'),
  ....

答案2

得分: 0

这是我的误解。

最初我以为如果第一个验证是电子邮件,如果电子邮件验证失败,它会在那里停止并返回错误。

实际上它会继续到下一个验证,最终进入您的错误处理程序。在错误处理程序中,有一个 errors.sort,在我的情况下,所以后来的错误消息被放在前面。

基本上这是一个 express-validator 不会在第一个错误处停止的功能。

英文:

It's my misunderstanding.

Initially i thought if the 1st validation is email, and if email validation fails, it'd stop there and return with error.

Actually it'll continue to the next validations and eventually goes into your error handler. In the error handler there's a errors.sort in my case, so a later error message was put in the beginning.

Basically it's a feature that express-validator won't stop at the 1st error.

huangapple
  • 本文由 发表于 2023年7月11日 09:23:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定