How can I conditionally add a function to a chain in JavaScript?
For instance, if modifier
is true, I want myKey
to be required using Joi.string().required()
. If it is false, then just use Joi.string()
:
function customJoi(modifier) {
return Joi.object({
myKey: Joi.string() //#If(modifier) .required() #EndIf
});
}
I am aware that I could achieve this without conditional statements by breaking it down into multiple steps. However, I'm interested in finding an elegant way to handle this for large objects.