I have this problem where I want one of my key in my Schema to contain an array of other types of "questions".
Below is my main schema.
const forumsSch = new mongoose.Schema({
summary: {
type: String,
required: true,
},
colorTheme: {
type: String,
enum: {
values: ['dark-Blue', 'dark-Yellow', 'light-Green'],
message: 'please enter a valid colorTheme',
},
},
colorCustom: {
type: [String],
},
questions: [
/*
(User request sample)
{
type: singleQuestion,
title: sample 1,
maxSelect: 5
},
{
type: multipleQuestion
title: sample 1,
imagesRepresent: true
}
*/
],
});
When user creates a document with its "questions" key as an array of questions (type specified in key "type") the corresponding schema automatically validates if each of them fit the requirements
below are the corresponding schemas
const singleQuestionSch = new mongoose.Schema({
title: {
type: String,
required: [true, 'Please specify title for your Question'], //validator
},
maxSelect: {
type: Number,
required: true,
},
});
const multipleQuestionSch = new mongoose.Schema({
title: {
type: String,
required: [true, 'Please specify title for your Question'], //validator
},
imagesRepresent: Boolean,
});
How do implement this?