ml-learner
ml-learner

Reputation: 93

Joi default value for empty array

I am struggling with the syntax for providing a default value to an empty and/or missing array key.

Joi.object({
    items: Joi.array().empty([]).items(Joi.number()).default([42]),
})

{ 
    items: []
}
or
{
}

In both cases I would want the default value of [42] to be substituted into the object. I'm assuming that I have to indicate that an empty array is invalid in some way, but I'm not sure how. I have also tried setting min(1) on items, but that doesn't work either (it flags the object as invalid, but does not substitute the default value).

Upvotes: 0

Views: 2835

Answers (1)

ml-learner
ml-learner

Reputation: 93

Sorry, posted this too soon. This works:

Joi.object({
    items: Joi.array().optional().empty(Joi.array().length(0)).default([42]),
})

Upvotes: 5

Related Questions