使用Joi检查是否存在一个密钥(必须为空),第二个密钥必须为空

时间:2019-07-30 18:10:59

标签: javascript node.js joi

我有一段使用Joi的代码,我需要验证一些属性,它们是可为空的,这意味着它们可以成为对象的一部分或不成为对象的一部分,这些是规则:

例如O和P 如果两者都为空也可以 如果O不为null,则必须为P 如果P不为空,则必须为O

与Q,R和S,T相同

function validateItemDataExcel(item) {
  const schema = Joi.object().keys({
    ...,
    O:Joi.string().allow(null),
    P:Joi.number().precision(2).allow(null).error(()=> 'Supplier 1 Price should have a number value (2 decimals)'),
    Q:Joi.string().allow(null),
    R:Joi.number().precision(2).allow(null).error(()=> 'Supplier 2 Price should have a number value (2 decimals)'),
    S:Joi.string().allow(null),
    T:Joi.number().precision(2).allow(null).error(()=> 'Supplier 3 Price should have a number value (2 decimals)')
  });
  return Joi.validate(item, schema);
}

我尝试使用.and()和.with(),但它似乎不起作用。

0 个答案:

没有答案