const schema = Joi.object().keys({
authors: Joi.array().items(Joi.string()).default([Joi.ref('$user')]),
});
This also will not work:
Joi.array().items(Joi.string()).default(Joi.ref(['$user']))
Joi.array().items(Joi.string()).default(Joi.ref('[$user]'))
Joi.array().items(Joi.string()).default(Joi.ref('$user')).single()
{authors: [null]}
{authors: ['defaultUser']}
@farwayer I assume you are attempting to validate some object with the schema(s) mentioned above, could you please provide some examples of your input data.
It was just example, but sure I can provide data that will fail:
const schema = Joi.object().keys({
authors: Joi.array().items(Joi.string()).default([Joi.ref('$user')]),
});
const data = {};
const {value} = Joi.validate(data, schema, {context: {user: 'defaultUser'}})
// values should be {authors: ['defaultUser']}
// but will be {authors: [null]}
There was a missing piece in joi to let you accomplish that which is why I consider this a bug. But for obvious reasons, joi is not going to deep explore your objects looking for references, but you can generate those defaults using a function like :
const schema = Joi.object().keys({
authors: Joi.array().items(Joi.string()).default(function (value, options) {
return [Joi.ref('$user')(value, options)];
}, 'Array with default user'),
});
Most helpful comment
There was a missing piece in joi to let you accomplish that which is why I consider this a bug. But for obvious reasons, joi is not going to deep explore your objects looking for references, but you can generate those defaults using a function like :