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

Is there a way to check a value like this with a Joi

so that I can check that it is an object with zero or more keys (of any name) and each one has string, number or boolean values

{
  dynamicallyNamedKey1: 'some value',
  dynamicallyNamedKey2: 4
}

      

+3


source to share


1 answer


You will want to use the method Joi

object().pattern()

. This is specifically for checking objects with unknown keys.

To match one or more datatypes on the same key, you will need alternatives().try()

(or just pass an array of Joi

types).



So the rule that suits your needs would be:

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

      

+3


source







All Articles