如何在 Joi 中检查是否需要 属性

How to check property is required or not in Joi

我想将 required 分配给 client_idclient_secret 如果其中一个 属性 不为空,我必须检查 Joi 分配它 required Joi 我如何检查这个

schema = Joi.Object().keys({
    account_id: Joi.string().required(),
    client_id: Joi.string(),   // in client_id or client_secret //
    client_secret: Joi.string() // one must be required I have to check for This in Joi
});

您可以使用 object.or 解决此问题

schema = Joi.Object().keys({
    account_id: Joi.string().required(),
    client_id: Joi.string(),   // in client_id or client_secret //
    client_secret: Joi.string() // one must be required I have to check for This in Joi
}).or('client_id', 'client_secret');