Javascript 根据另一个键的输入,根据需要进行一次输入
我正在使用它进行模式验证。在下面的模式中,当Javascript 根据另一个键的输入,根据需要进行一次输入,javascript,hapijs,joi,Javascript,Hapijs,Joi,我正在使用它进行模式验证。在下面的模式中,当type为jobType.MBR时,我希望input\u file为required类型,否则file\u name必须保持required const jobObjectSchema = { type: Joi.string().valid(jobType.MBR, jobType.MP4).required(), file_name: Joi.string().required(), input_file: Joi.string() }
type
为jobType.MBR
时,我希望input\u file
为required类型,否则file\u name
必须保持required
const jobObjectSchema = {
type: Joi.string().valid(jobType.MBR, jobType.MP4).required(),
file_name: Joi.string().required(),
input_file: Joi.string()
};
我如何才能做到这一点?使用
Joi
any()。当
时
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()
})
};