Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript joi:不返回自定义错误,ABORTEARY设置为false_Javascript_Node.js_Hapijs_Joi - Fatal编程技术网

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

Javascript joi:不返回自定义错误,ABORTEARY设置为false,javascript,node.js,hapijs,joi,Javascript,Node.js,Hapijs,Joi,我无法让这个joi验证返回所有错误,就像它对默认错误所做的那样 因此,我在这里为每个字段设置单独的自定义错误: const schema = Joi.object().keys({ a: Joi.string().error(new Error('must be string')), b: Joi.number().error(new Error('must be number')) }); 然后,当Aborterly设置为false进行验证时,它只返回将遇到的第一个错误 J

我无法让这个joi验证返回所有错误,就像它对默认错误所做的那样

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

const schema = Joi.object().keys({
    a: Joi.string().error(new Error('must be string')), 
    b: Joi.number().error(new Error('must be number'))
});
然后,当Aborterly设置为false进行验证时,它只返回将遇到的第一个错误

Joi.validate({a: 1, b: false}, schema, {abortEarly: false})
返回的错误如下所示

{ error: [Error: must be string], value: { a: 1, b: false }}
当它应该以某种方式返回所有错误时


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

嗯,我想我找到了答案。我的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" };
       }
    });
  })
开关箱还有另一种解决方案:

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.`
参考文献: