I am defining a Joi schema for an object with some fields for social profiles. The goal is to ensure that all of the fields are present and that at least one of them is a non-empty string, but to allow the others to be null
.
Here is what I have so far:
const socialUrl = joi.string().min(1).alphanum().allow(null);
const schema = joi.object({
facebook : socialUrl.required(),
twitter : socialUrl.required(),
youtube : socialUrl.required()
}).required()
This works great except that the following object is considered valid:
{
facebook : null,
twitter : null,
youtube : null
}
Using min() and or() on the object doesn't help because the fields exist, they're just the wrong type.
It would be pretty trivial to do an extra loop myself afterwards and check for the specific case of all fields being null
, but that is not ideal.
This is being used for payload validation where it would be nice to know that the client understands the full schema, hence the desire that all of the fields be present. Maybe that is not worth it. I do get the sense that I could use some verbose when() code to get the job done, but it seems like at that point I might as well just make my own loop. Hoping to be proven wrong!