【问题标题】:Making one input as required based on the input of another key根据另一个键的输入,按要求进行一个输入
【发布时间】:2018-02-01 18:25:40
【问题描述】:

我正在使用JOI 进行架构验证。在以下架构中,我希望 typejobType.MBR 时,input_file 是必需的类型,否则 file_name 必须保持为 required 类型

const jobObjectSchema = {
  type: Joi.string().valid(jobType.MBR, jobType.MP4).required(),
  file_name: Joi.string().required(),
  input_file: Joi.string()
};

我该怎么做?

【问题讨论】:

    标签: javascript hapijs joi


    【解决方案1】:

    使用Joiany().when

    const jobObjectSchema = {
      type: Joi.string().valid(jobType.MBR, jobType.MP4).required(),
      file_name: Joi.any().when('type', {
        is: jobType.MBR, 
        then: Joi.string().optional(),
        otherwise: Joi.string().required()
      }),
      input_file: Joi.any().when('type', {
        is: jobType.MBR, 
        then: Joi.string().required(),
        otherwise: Joi.string().optional()
      })
    };
    

    【讨论】:

      猜你喜欢
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多