How to extend a Joi schema?
You can use object.keys([schema])
, which
Sets or extends the allowed object keys where:
schema
- optional object where each key is assigned a joi type object. Ifschema
is{}
no keys allowed. Ifschema
isnull
orundefined
, any key allowed. Ifschema
is an object with keys, the keys are added to any previously defined keys (but narrows the selection if all keys previously allowed). Defaults to 'undefined' which allows any child key.
Example:
const base = Joi.object().keys({
a: Joi.number(),
b: Joi.string()
});
// Validate keys a, b and c.
const extended = base.keys({
c: Joi.boolean()
});
Simply you can spread the user
inside ownerSchema
:
const ownerSchema = {
...user,
/* owner specific fields goes here */
};