JOI :allow null values in array

I know what i'm posting is not what you are looking for, but as i was ran into similar type of problem.

so my problem was: I do not want to allow empty array in my object

my solution:

// if you have array of numbers

key: joi.array().items(joi.number().required()).strict().required()

// if you have array of strings

key: joi.array().items(joi.string().required()).strict().required()

If you want to allow the array to be null use:

Joi.array().items(Joi.string()).allow(null);

If you want to allow null or whitespace strings inside the array use:

Joi.array().items(Joi.string().allow(null).allow(''));

Example:

const Joi = require('joi');

var schema = Joi.array().items(Joi.string()).allow(null);

var arr = null;

var result = Joi.validate(arr, schema); 

console.log(result); // {error: null}

arr = ['1', '2'];

result = Joi.validate(arr, schema);

console.log(result); // {error: null}


var insideSchema = Joi.array().items(Joi.string().allow(null).allow(''));

var insideResult = Joi.validate(['1', null, '2'], insideSchema);

console.log(insideResult);

the very short answer is:

name: Joi.string().allow(null)