'Joi validator body accepts object or array

I would like to use Joi to validate an object or an array of objects.

I have tried doing this but it looks like it's checking for both an Array of entities and entity. I would like it to only check one or the other. entity is also a joi object.

Joi.alternatives().try(Joi.array().items(entity), entity)

For example, I want both of these bodies to be validated

  1. array of entities
    [
    {name: "joe"},
    {name: "tim"},
    {name: "bob"}
    ]
  1. single entity
    {name: "joe"}

What's the best way to do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source