Joi Validations: If object matches the schema validate against it from multiple items

There are some typos in your files and validators. First of Joi.array is a function and needs to be called as one. // This is incorrect data2: Joi.array … additional: Joi.array // Should be changed to this where array is invoked as a function data2: Joi.array() … additional: Joi.array() Additionally, I’ve added a fourth validator … Read more

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()))

Node.js + Joi how to display a custom error messages?

Original answer: The current way (I personally find it better) is to use .messages() (or .prefs({messages})). const Joi = require(‘@hapi/joi’); const joiSchema = Joi.object({ a: Joi.string() .min(2) .max(10) .required() .messages({ ‘string.base’: `”a” should be a type of ‘text’`, ‘string.empty’: `”a” cannot be an empty field`, ‘string.min’: `”a” should have a minimum length of {#limit}`, ‘any.required’: … Read more