【问题标题】:Custom string validation with Joi.js使用 Joi.js 进行自定义字符串验证
【发布时间】:2021-10-19 00:11:56
【问题描述】:

我的 API 中有一个输入,我需要对其进行验证,看看它是否是我想要的两个字符串之一。有没有办法用 Joi.js 验证字符串

type DummyValidatorInput = {
  firstInput: string;
  secondInput: string;
}

export const DummyValidator = (
  input: DummyValidatorInput,
): Joi.ValidationResult => {
  return Joi.object({
    firstInput: Joi.string().min(5).max(64).required(),
    secondInput: Joi.string().required()
  }).validate(input);
};

我想验证如果secondInputcustomerTypeOne 字符串或customerTypeTwo 字符串,Joi 可以吗? 任何想法或建议表示赞赏:)

【问题讨论】:

    标签: validation joi


    【解决方案1】:

    您是否尝试过使用any.valid

    secondInput: Joi.string().valid('customerTypeOne', 'customerTypeTwo').required()
    

    【讨论】:

      猜你喜欢
      • 2022-11-25
      • 2021-07-03
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多