I have a joi schema called user
const user = {
firstName: Joi.string()
.min(2)
.max(50)
.required()
.label('First Name'),
lastName: Joi.string()
.min(3)
.max(50)
.required()
.label('Last Name'),
email: Joi.string()
.allow('')
.email({ minDomainAtoms: 2 })
.max(100)
.label('Email Address'),
}
I had another one called owner
const ownerSchema = {
firstName: Joi.string()
.min(2)
.max(50)
.required()
.label('First Name'),
lastName: Joi.string()
.min(3)
.max(50)
.required()
.label('Last Name'),
email: Joi.string()
.allow('')
.email({ minDomainAtoms: 2 })
.max(100)
.label('Email Address'),
number: Joi.string()
.regex(/[0-9]/)
.length(10)
.required()
.label('Phone Number'),
dateOfBirth: Joi.date(),
kycDetails: Joi.array()
.items(schemaKyc)
.required(),
bankDetails: Joi.array()
.items(schemaBank)
.required(),
licenceDetails: Joi.array()
.items(schemaLicence)
.required(),
insuranceDetails: Joi.array()
.items(schemaInsurance)
.required()
};
As you can see the both have three fields in common I want to be able to use the user schema in owner and whenever I make changes to the user I want it to reflect in the owner as well.
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 */
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With