Search code examples
javascriptreactjsjoi

How to extend a Joi schema?


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.


Solution

  • 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. If schema is {} no keys allowed. If schema is null or undefined, any key allowed. If schema 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()
    });