Is there a way to validate dynamic key names in a Joi schema?

You’re going to want to use Joi‘s object().pattern() method. It’s specifically for validating objects with unknown keys.

To match against one or more datatypes on a single key you’ll need alternatives().try() (or simply pass an array of Joi types).

So the rule to match your needs would be:

Joi.object().pattern(/^/, Joi.alternatives().try(Joi.string(), Joi.number(), Joi.boolean()))

Leave a Comment