File size: 10,089 Bytes
9ba737d 450b0f4 bec3643 9ba737d dc9a361 9ba737d 9a99e04 fed8602 0909626 9ba737d aa644e4 450b0f4 9ba737d 450b0f4 9ba737d 450b0f4 9ba737d 450b0f4 bec3643 450b0f4 bec3643 450b0f4 bec3643 450b0f4 bec3643 450b0f4 bec3643 450b0f4 bec3643 450b0f4 bec3643 450b0f4 9ba737d 450b0f4 9ba737d 450b0f4 9ba737d 450b0f4 fed8602 450b0f4 9ba737d 450b0f4 9ba737d 450b0f4 9ba737d 450b0f4 dc9a361 9ba737d 450b0f4 0909626 9ba737d 450b0f4 9ba737d 450b0f4 9a99e04 450b0f4 9a99e04 fed8602 9ba737d 450b0f4 9ceb4c0 aa644e4 497adc2 aa644e4 9ceb4c0 b185a6d 9ceb4c0 450b0f4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | import { BadRequestException } from "@nestjs/common";
const SUPPORTED_COMPONENT_TYPES = new Set([
"component_group",
"dialogue",
"media",
"markdown",
"target_matching",
"category_sorting",
"mindmap_reveal",
"mcq",
"quiz_sequence",
"multi_select",
"matching_columns",
"true_false",
"sequence_sorting",
"chain_sorting",
"knowledge_piece",
"map_target_matching",
"progression_spiral",
"timeline_explorer",
"hotspot_gallery",
"shinkei_matching",
"final_summary",
"fill_in_blanks",
]);
function requireArray(value: any, label: string) {
if (!Array.isArray(value)) {
throw new BadRequestException(`${label} must be an array`);
}
}
function requireString(value: any, label: string) {
if (typeof value !== "string" || value.trim() === "") {
throw new BadRequestException(`${label} must be a non-empty string`);
}
}
function requireObject(value: any, label: string) {
if (!value || typeof value !== "object" || Array.isArray(value)) {
throw new BadRequestException(`${label} must be a JSON object`);
}
}
export class NodeSchemaValidator {
static validateLessonFlow(data: any) {
requireArray(data, "lessonFlow");
const ids = new Set<string>();
data.forEach((component: any, idx: number) => {
this.validateComponent(component, `lessonFlow[${idx}]`, ids);
});
}
private static validateComponent(
component: any,
path: string,
ids: Set<string>,
isChild = false,
) {
requireObject(component, path);
requireString(component.id, `${path}.id`);
requireString(component.type, `${path}.type`);
if (ids.has(component.id)) {
throw new BadRequestException(`${path}.id duplicates "${component.id}"`);
}
ids.add(component.id);
if (!SUPPORTED_COMPONENT_TYPES.has(component.type)) {
throw new BadRequestException(
`${path}.type "${component.type}" is not supported`,
);
}
if (isChild && component.type === "component_group") {
throw new BadRequestException(`${path}.type cannot be component_group`);
}
requireObject(component.config, `${path}.config`);
this.validateComponentConfig(component, path, ids);
if (component.completionRule) {
requireObject(component.completionRule, `${path}.completionRule`);
requireString(
component.completionRule.type,
`${path}.completionRule.type`,
);
}
}
private static validateComponentConfig(
component: any,
path: string,
ids: Set<string>,
) {
const config = component.config;
const label = `${path}.config`;
if (component.type === "component_group") {
requireArray(config.components, `${label}.components`);
if (config.components.length === 0) {
throw new BadRequestException(
`${label}.components must contain at least one child component`,
);
}
config.components.forEach((child: any, childIdx: number) => {
this.validateComponent(
child,
`${label}.components[${childIdx}]`,
ids,
true,
);
});
}
if (component.type === "dialogue") {
requireArray(config.lines, `${label}.lines`);
config.lines.forEach((line: any, lineIdx: number) => {
requireString(line.text, `${label}.lines[${lineIdx}].text`);
});
}
if (component.type === "media") {
requireString(config.url, `${label}.url`);
}
if (component.type === "markdown") {
requireString(config.content, `${label}.content`);
}
if (
component.type === "target_matching" ||
component.type === "map_target_matching"
) {
requireArray(config.targets, `${label}.targets`);
requireArray(config.items, `${label}.items`);
}
if (component.type === "category_sorting") {
requireArray(config.categories, `${label}.categories`);
requireArray(config.cards, `${label}.cards`);
}
if (component.type === "mcq") {
requireString(config.question, `${label}.question`);
requireArray(config.options, `${label}.options`);
if (
!config.options.some(
(option: any) => option.isCorrect === true || option.correct === true,
)
) {
throw new BadRequestException(
`${label}.options must contain at least one correct option`,
);
}
}
if (component.type === "quiz_sequence") {
requireArray(config.questions, `${label}.questions`);
config.questions.forEach((question: any, questionIdx: number) => {
requireString(
question.question || question.prompt,
`${label}.questions[${questionIdx}].question`,
);
requireArray(
question.options,
`${label}.questions[${questionIdx}].options`,
);
if (
typeof question.correctIndex !== "number" &&
!question.options.some(
(option: any) =>
option?.isCorrect === true || option?.correct === true,
)
) {
throw new BadRequestException(
`${label}.questions[${questionIdx}] must define correctIndex or a correct option`,
);
}
});
}
if (component.type === "multi_select") {
requireString(config.question, `${label}.question`);
requireArray(config.options, `${label}.options`);
if (
!config.options.some(
(option: any) => option.isCorrect === true || option.correct === true,
)
) {
throw new BadRequestException(
`${label}.options must contain at least one correct option`,
);
}
}
if (component.type === "matching_columns") {
requireArray(config.leftColumn, `${label}.leftColumn`);
requireArray(config.rightColumn, `${label}.rightColumn`);
requireArray(config.correctPairs, `${label}.correctPairs`);
}
if (component.type === "shinkei_matching") {
requireArray(config.pairs, `${label}.pairs`);
if (config.pairs.length === 0) {
throw new BadRequestException(
`${label}.pairs must contain at least one pair`,
);
}
}
if (component.type === "true_false") {
requireString(config.statement, `${label}.statement`);
if (typeof config.correctAnswer !== "boolean") {
throw new BadRequestException(
`${label}.correctAnswer must be a boolean`,
);
}
}
if (
component.type === "sequence_sorting" ||
component.type === "chain_sorting"
) {
requireArray(config.items, `${label}.items`);
}
if (component.type === "knowledge_piece") {
requireString(config.label || component.title, `${label}.label`);
if (config.takeaways !== undefined) {
requireArray(config.takeaways, `${label}.takeaways`);
}
}
if (component.type === "progression_spiral") {
requireArray(config.milestones, `${label}.milestones`);
}
if (component.type === "timeline_explorer") {
requireArray(config.periods, `${label}.periods`);
}
if (component.type === "hotspot_gallery") {
requireArray(config.items, `${label}.items`);
}
if (component.type === "final_summary") {
if (config.keyTakeaways !== undefined) {
requireArray(config.keyTakeaways, `${label}.keyTakeaways`);
}
}
if (component.type === "fill_in_blanks") {
requireString(config.textWithBlanks, `${label}.textWithBlanks`);
requireArray(config.blanks, `${label}.blanks`);
config.blanks.forEach((blank: any, blankIdx: number) => {
requireString(blank.id, `${label}.blanks[${blankIdx}].id`);
requireString(
blank.correctAnswer,
`${label}.blanks[${blankIdx}].correctAnswer`,
);
});
}
if (component.type === "mindmap_reveal") {
requireArray(config.nodes, `${label}.nodes`);
// Validate each node supports both legacy format and new front/back format
config.nodes.forEach((node: any, nodeIdx: number) => {
if (!node.id) {
throw new BadRequestException(
`${label}.nodes[${nodeIdx}].id is required`,
);
}
// Must have either legacy label/detail or new front/back
const hasLegacy = node.label !== undefined || node.detail !== undefined;
const hasNewFormat =
node.front !== undefined || node.back !== undefined;
if (!hasLegacy && !hasNewFormat) {
throw new BadRequestException(
`${label}.nodes[${nodeIdx}] must have either label/detail or front/back fields`,
);
}
});
// Validate optional layout config
if (config.layoutConfig) {
const validTypes = ["vertical", "horizontal", "matrix", "custom"];
if (!validTypes.includes(config.layoutConfig.type)) {
throw new BadRequestException(
`${label}.layoutConfig.type must be one of: ${validTypes.join(", ")}`,
);
}
}
}
}
/**
* Validate the lessonMedia array (center column media items).
* Each item must have id, type, and url.
*/
static validateLessonMedia(data: any) {
if (data === null || data === undefined) return; // Optional field
requireArray(data, "lessonMedia");
const ids = new Set<string>();
data.forEach((item: any, idx: number) => {
requireObject(item, `lessonMedia[${idx}]`);
requireString(item.id, `lessonMedia[${idx}].id`);
requireString(item.type, `lessonMedia[${idx}].type`);
requireString(item.url, `lessonMedia[${idx}].url`);
if (ids.has(item.id)) {
throw new BadRequestException(
`lessonMedia[${idx}].id duplicates "${item.id}"`,
);
}
ids.add(item.id);
const validTypes = ["video", "image"];
if (!validTypes.includes(item.type)) {
throw new BadRequestException(
`lessonMedia[${idx}].type must be one of: ${validTypes.join(", ")}`,
);
}
});
}
static validateNode(lessonFlow: any) {
this.validateLessonFlow(lessonFlow);
}
}
|