Reputation: 53
I'm working with Mongoose to create a StepperDataSchema that contains a celebratorDetails field. This field can have one of two possible sub-schema structures:
1.BirthdayCelebratorDetailsSchema
2.WeddingCelebratorDetailsSchema
Here are the definitions of my schemas:
// First Custom Schema
const BirthdayCelebratorDetailsSchema = new Schema({
name: { type: String, required: true },
celebratorType: {
type: String,
enum: Object.values(CelebratorTypes),
required: true,
},
age: {
type: Number,
required: true,
min: 1,
max: 120,
},
});
// Second Custom Schema
const WeddingCelebratorDetailsSchema = new Schema({
firstCelebrator: {
name: { type: String, required: true },
type: {
type: String,
enum: Object.values(WeddingCelebratorTypes),
required: true,
},
parentsNames: { type: String, required: true },
},
secondCelebrator: {
name: { type: String, required: true },
type: {
type: String,
enum: Object.values(WeddingCelebratorTypes),
required: true,
},
parentsNames: { type: String, required: true },
},
});
Currently, in my StepperDataSchema
, I'm using Schema.Types.Mixed
to represent celebratorDetails
:
const StepperDataSchema = new Schema({
celebratorDetails: {
type: Schema.Types.Mixed, // ❌ I want to use the custom schemas here
required: true,
},
// other fields...
});
I want to strictly type celebratorDetails
to only allow either BirthdayCelebratorDetailsSchema
or WeddingCelebratorDetailsSchema
, instead of the generic Schema.Types.Mixed
Any guidance or examples on how to achieve this would be greatly appreciated!
Upvotes: 0
Views: 25