如何为快速验证器返回的错误添加额外的属性

时间:2019-11-15 00:25:36

标签: node.js validation express express-validator

当我使用express-validator和validate字段时,总是得到带有value,msg,param和location属性的错误响应。如果我想在其中添加额外的属性(如说代码以将其传达给客户端),该怎么办。

例如:

check('name').not().isEmpty().withMessage('Name must have more than 5 characters')

我知道

{ value: undefined, msg: 'Name must have more than 5 characters', param 'name', location: 'body'}

// What I am looking to achieve is this

{ value: undefined, msg: 'Name must have more than 5 characters', code: 'NAME_MANDATORY' param 'name', location: 'body'}

How do I achive this with express-validator?

1 个答案:

答案 0 :(得分:0)

您可以尝试为withMessage提供类似的功能

withMessage((value, { req, location, path }) => {
    return  { value, location, path, otherkey:'some message' };
  })