根据另一个键的输入根据需要进行一次输入

时间:2018-02-01 10:17:31

标签: javascript hapijs joi

我正在使用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() }; 类型

select top 2 from (select TutionFee from TBLStudentFeeRecord 
    where StudentId = 1001
    order by TutionFee desc )

我该怎么做?

1 个答案:

答案 0 :(得分:1)

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