如何使用 JOI 处理来自自定义验证的错误?

how to handle errors from a custom validation with JOI?

我想为使用 JOI 的自定义验证设置自定义错误消息,但这不起作用

 field: Joi.string()
.required()
.custom((value, helper) => {
  if (!isValidField(value)) {
    return helper.message({'any.custom': 'This value is Invalid'});
  }
  return value;
}),

我用这段代码解决了问题

field: Joi.string()
.required()
.custom((value, helper) => {
  if (isValidField(value)) {
    return value;
  }
  return helper.message({ custom: 'This value is Invalid' });
})

找到关于这个 link 的答案:https://github.com/sideway/joi/issues/2235