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

时间:2017-04-05 04:41:56

标签: javascript node.js hapijs joi

我无法通过此joi验证来返回所有错误,就像它对默认错误所做的那样。

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

eval

然后在使用abortEarly设置为false进行验证时,它只返回它将遇到的第一个错误。

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

返回的错误是这样的,

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

应该以某种方式返回所有错误。

我是否使用abortEarly错误或者是否需要在返回所有自定义错误时执行某个过程?提前感谢您的回复。

2 个答案:

答案 0 :(得分:0)

嗯,我想我找到了答案。我的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]'

答案 1 :(得分:0)

我还有另一种方法来检查每个验证错误。 如果您有一个验证,则无论条件如何,您都可以这样做:

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" };
     }
  });
})
  

请注意,如果未指定验证错误之一,则说明您未定义 err.type

您可以在消息中使用 err.context 来动态显示标签,限制,最大值,...。

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

引用:Joi Document