我正在使用JOI用于模式验证。在以下架构中,当 type
为 jobType.MBR
时,我希望 input_file
具有所需的类型,否则 file_name
必须保持类型必需
const jobObjectSchema = {
type: Joi.string().valid(jobType.MBR, jobType.MP4).required(),
file_name: Joi.string().required(),
input_file: Joi.string()
};
我该怎么做?
最佳答案
使用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()
})
};
关于javascript - 根据另一键的输入,根据需要进行一个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48560274/