如何检查两个属性是否等于 Joi 中另一个对象的属性?

How to check if two properties are equal to another object's properties in Joi?

是否可以让 Joi 检查如果 source.listId === destination.listId && source.index === destination.index 然后 return 一个错误?

我的例子:

const schema = Joi.object({
  source: Joi.object().keys({
    listId: Joi.string()
      .length(24)
      .required()
      .when('...destination.index', {
        is: Joi.equal(Joi.ref('index')),
        then: Joi.disallow(Joi.ref('...destination.listId'))
      }),
    index: Joi.number()
      .integer()
      .positive()
      .required()
  }),
  destination: Joi.object().keys({
    listId: Joi.string()
      .length(24)
      .required(),
    index: Joi.number()
      .integer()
      .positive()
      .required()
  })
});

已解决

我还必须用 when 验证 source.index 属性:

const schema = Joi.object({
  source: Joi.object().keys({
    listId: Joi.string()
      .length(24)
      .required()
      .when('...destination.index', {
        is: Joi.equal(Joi.ref('index')),
        then: Joi.disallow(Joi.ref('...destination.listId'))
      }),
    index: Joi.number()
      .integer()
      .positive()
      .required()
      .when('...destination.listId', {
        is: Joi.equal(Joi.ref('listId')),
        then: Joi.disallow(Joi.ref('...destination.index'))
      })
  }),
  destination: Joi.object().keys({
    listId: Joi.string()
      .length(24)
      .required(),
    index: Joi.number()
      .integer()
      .positive()
      .required()
  })
});