Search code examples
node.jsnpmoption-typerequiredjoi

How to check property is required or not in Joi


I want to assign required to either client_id or client_secret I have to check in Joi if one of these property is not empty assign it required Joi how can i check this

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
});

Solution

  • You can use object.or to resolve this issue

    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');