Extending Schemas
Schemas can inherit fields and options from a parent schema by calling the extend method.
Basic example
const baseSchema = new Schema(
{
id: { constant: true, value: generateId },
dob: { default: "" },
name: { default: "" },
},
{ timestamps: true },
);
const extendedSchema = baseSchema.extend(
{
name: { default: "default-name" },
},
{ timestamps: { createdAt: "cAt" }, remove: "dob" },
);
Rules
- To overwrite a property, redefine it in the extension definitions.
- The parent schema's options are inherited by default.
postValidate,shouldUpdate, and lifecycle options are not inherited.- Use
useParentOptions: falseto prevent option inheritance. - Use the
removeoption to drop parent properties.