joi:不返回自定义错误,abortEarly 设置为 false

joi: Custom errors are not returned, abortEarly is set to false

我无法像处理默认错误那样对所有错误进行 joi 验证return。

所以我在这里为每个字段设置单独的自定义错误:

const schema = Joi.object().keys({
    a: Joi.string().error(new Error('must be string')), 
    b: Joi.number().error(new Error('must be number'))
});

然后在将 abortEarly 设置为 false 进行验证时,它只会 return 遇到第一个错误。

Joi.validate({a: 1, b: false}, schema, {abortEarly: false})

错误returned是这样的,

{ error: [Error: must be string], value: { a: 1, b: false }}

什么时候应该return以某种方式解决所有错误。

我是否错误地使用了 abortEarly 或是否需要在 returning 所有自定义错误中完成一个过程?预先感谢您的任何回复。

好吧,我想我找到了答案。我的 joi 库没有更新,所以我把它从 10.2.x 升级到了 10.4.1。我在文档中看到的一些功能在我尝试旧版本时不起作用,包括我所做的解决方案。

我试过使用这个模式并且有效:

const schema = Joi.object().keys({
    a: Joi.string().error(() => 'must be string'), 
    b: Joi.number().error(() => 'must be number')
});

像这样:

{ [ValidationError: child "a" fails because [must be string]. child "b" fails because [must be number]]
  isJoi: true,
  name: 'ValidationError',
  details: 
   [ { message: '"a" must be a string',
       path: 'a',
       type: 'string.base',
       context: [Object] },
     { message: '"b" must be a number',
       path: 'b',
       type: 'number.base',
       context: [Object] } ],
  _object: { a: 1, b: false },
  annotate: [Function] }

然后我将解析 error.message 以获取所有错误消息并进行处理。

'child "a" fails because [must be string]. child "b" fails because [must be number]'

我还有另一种方法来检查每个验证错误。 如果你有一个验证,不管条件是什么,你可以这样做:

username: Joi.string() // It has to be string
  .label("Username") // The label
  .error(new Error('It is whatever error')) // Custom general error

你也可以用箭头函数来做到这一点:

username: Joi.string() // It has to be string
  .label("Username") // The label
  .error(() => 'It is whatever error') // Custom general error

但是如果有一些验证参数和错误,我们有这些解决方案:

password: Joi.string()  // It has to be string
  .min(8) // It has to have at least 8 characters
  .required() // It has to have a value
  .label("Password") // The label
  .error(errors => {
    return errors.map(err => { // Here we map the errors (ES6) discover within an array
       if (err.type === "string.min") { // Check the type of error e.g: 'string.min,any.empty,number.min,...'
         return { message: "The length of the parameter should be more than 8 characters" }; // Which message we want to display
       } else {
         return { message: "another validation error" };
       }
    });
  })

Switch Case 还有另一种解决方案:

password: Joi.string()  // It has to be string
  .min(8) // It has to have at least 8 characters
  .required() // It has to have a value
  .label("Password") // The label
  .error(errors => {
    return errors.map(err => { // Here we map the errors (ES6) discover within an array
     switch (err.type) {
       case "string.min":
          return { message: "The length of the parameter should be more than 8 characters" }; // Which message we want to display
       case "any.empty":
          return { message: "The parameter should have a value" };
     }
  });
})

Please noticed that if one of the validation errors did not specify you have error that err.type is undefined.

您可以在消息中使用 err.context 以动态显示标签、限制、最大值...:[=​​17=]

message: `${err.context.label} must have at least ${err.context.limit} characters.`

参考:Joi Document