I didn't succeed in finding it on the web,
but I can't imagine this is not possible through joi.
Considering this very simple schema below...
Joi.object({
field_1: Joi.string().required(),
field_2: Joi.string().required(),
})
...and considering this provided object:
(note that field_2 key and value are missing)
{field_1: "a string"}
I'd like joi to generate this complete object:
{field_1: "a string",
field_2: "a default value"}
How to do so?
Thanks
You can define your own validation function for a key.
If value is empty return "any string".
For example:
const method = (value) => {
if (value === '')
return "default string"
return value
}
Joi.object({
field_1: Joi.string().custom(method, 'custom validation'),
field_2: Joi.string().required(),
})
For more information: https://joi.dev/api/?v=17.6.0