【问题标题】:how to handle errors from a custom validation with JOI?如何使用 JOI 处理自定义验证中的错误?
【发布时间】:2021-11-06 09:24:58
【问题描述】:

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

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

【问题讨论】:

    标签: javascript typescript validation joi


    【解决方案1】:

    我用这段代码解决了问题

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

    在此链接上找到答案:https://github.com/sideway/joi/issues/2235

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 2020-09-13
      • 2019-07-23
      • 1970-01-01
      • 2019-03-21
      • 1970-01-01
      • 2017-09-06
      相关资源
      最近更新 更多