From b17a241274181d28175e905841ec8f97d7db9106 Mon Sep 17 00:00:00 2001 From: "Rishi . K" Date: Thu, 12 Nov 2026 20:23:01 +0530 Subject: [PATCH] fix(interview): improve adaptive session schema validation Improved adaptive question schema validation. Added timestamps to individual interview questions. Added trimming for text fields. Added topic validation to prevent empty topic lists. Added minimum and maximum question limits. Added integer validation for maxQuestions. Prevented question arrays from exceeding the configured limit during document validation. Improved feedback score validation. Added compound index for user and session status. Improved default values and schema consistency. Maintained existing model export and field compatibility. --- backend/models/AdaptiveInterviewSession.js | 174 +++++++++++++++++---- 1 file changed, 146 insertions(+), 28 deletions(-) diff --git a/backend/models/AdaptiveInterviewSession.js b/backend/models/AdaptiveInterviewSession.js index 15ad30d8..cd3ac90e 100644 --- a/backend/models/AdaptiveInterviewSession.js +++ b/backend/models/AdaptiveInterviewSession.js @@ -1,34 +1,152 @@ + const mongoose = require("mongoose"); -const adaptiveQuestionSchema = new mongoose.Schema({ - questionText: { type: String, required: true }, - topic: { type: String, required: true }, - difficulty: { type: String, enum: ["Easy", "Medium", "Hard"], required: true }, - userAnswer: { type: String, default: "" }, - feedback: { - correctnessScore: { type: Number, min: 0, max: 100, default: null }, - explanationScore: { type: Number, min: 0, max: 100, default: null }, - approachFeedback: { type: String, default: "" }, - generalFeedback: { type: String, default: "" } +const adaptiveQuestionSchema = new mongoose.Schema( + { + questionText: { + type: String, + required: true, + trim: true + }, + topic: { + type: String, + required: true, + trim: true + }, + difficulty: { + type: String, + enum: ["Easy", "Medium", "Hard"], + required: true + }, + userAnswer: { + type: String, + default: "", + trim: true + }, + feedback: { + correctnessScore: { + type: Number, + min: 0, + max: 100, + default: null + }, + explanationScore: { + type: Number, + min: 0, + max: 100, + default: null + }, + approachFeedback: { + type: String, + default: "", + trim: true + }, + generalFeedback: { + type: String, + default: "", + trim: true + } + }, + overallScore: { + type: Number, + min: 0, + max: 100, + default: null + }, + isAnswered: { + type: Boolean, + default: false + } + }, + { timestamps: true } +); + +const adaptiveInterviewSessionSchema = new mongoose.Schema( + { + user: { + type: mongoose.Schema.Types.ObjectId, + ref: "User", + required: true, + index: true + }, + role: { + type: String, + required: true, + trim: true + }, + experienceLevel: { + type: String, + required: true, + trim: true + }, + topics: { + type: [{ + type: String, + trim: true + }], + required: true, + validate: { + validator: value => value.length > 0, + message: "At least one topic is required." + } + }, + status: { + type: String, + enum: ["in-progress", "completed"], + default: "in-progress" + }, + currentDifficulty: { + type: String, + enum: ["Easy", "Medium", "Hard"], + default: "Medium" + }, + maxQuestions: { + type: Number, + min: 1, + max: 50, + default: 5, + validate: { + validator: Number.isInteger, + message: "maxQuestions must be an integer." + } + }, + questions: { + type: [adaptiveQuestionSchema], + default: [], + validate: { + validator: function (value) { + return value.length <= this.maxQuestions; + }, + message: "Questions cannot exceed maxQuestions." + } + }, + finalReport: { + totalScore: { + type: Number, + min: 0, + max: 100, + default: null + }, + improvementAreas: { + type: [String], + default: [] + }, + topicPerformance: { + type: mongoose.Schema.Types.Mixed, + default: {} + } + } }, - overallScore: { type: Number, min: 0, max: 100, default: null }, - isAnswered: { type: Boolean, default: false } + { timestamps: true } +); + +adaptiveInterviewSessionSchema.index({ + user: 1, + status: 1 }); -const adaptiveInterviewSessionSchema = new mongoose.Schema({ - user: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true, index: true }, - role: { type: String, required: true }, - experienceLevel: { type: String, required: true }, - topics: { type: [String], required: true }, - status: { type: String, enum: ["in-progress", "completed"], default: "in-progress" }, - currentDifficulty: { type: String, enum: ["Easy", "Medium", "Hard"], default: "Medium" }, - maxQuestions: { type: Number, default: 5 }, - questions: [adaptiveQuestionSchema], - finalReport: { - totalScore: { type: Number, default: null }, - improvementAreas: { type: [String], default: [] }, - topicPerformance: { type: mongoose.Schema.Types.Mixed, default: {} } - } -}, { timestamps: true }); +module.exports = mongoose.model( + "AdaptiveInterviewSession", + adaptiveInterviewSessionSchema +); -module.exports = mongoose.model("AdaptiveInterviewSession", adaptiveInterviewSessionSchema);