小编Shi*_*rma的帖子

Express-validator:我们如何验证对象密钥?

我遇到了快速验证器使用两个键验证对象的问题。我的方法如下。

  check('contact.code')
    .trim()
    .isNumeric()
    .withMessage('Country code must be numeric.')
    .bail()
    .isLength({min: 1, max: 4})
    .withMessage('Invalid country code.')
    .bail(),
  check('contact.number')
    .trim()
    .isNumeric()
    .withMessage('Phone number must be numeric.')
    .bail()
    .isLength({max: 10, min: 10})
    .withMessage('Phone number must be 10 digits long.')
    .bail(),
Run Code Online (Sandbox Code Playgroud)

req.body中,我将我的联系人发送为, contact: {"code": "91", "number":"9087654321"} 但我收到错误:

{
    "errors": [
        {
            "value": "",
            "msg": "Country code must be numeric.",
            "param": "contact.code",
            "location": "body"
        },
        {
            "value": "",
            "msg": "Phone number must be numeric.",
            "param": "contact.number",
            "location": "body"
        }
    ]
} …
Run Code Online (Sandbox Code Playgroud)

node.js express joi express-validator

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

express ×1

express-validator ×1

joi ×1

node.js ×1