根据另一个键的输入按要求进行一个输入

Making one input as required based on the input of another key

我正在使用 JOI 进行架构验证。在下面的架构中,我希望 input_filetypejobType.MBR 时属于必需类型,否则 file_name 必须保持 required

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

我该怎么做?

使用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()
  })
};