@mathscan/math-exercise-engine 1.0.9 → 1.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -437,13 +437,16 @@ class MathExerciseParser extends CstParser {
437
437
  this.OPTION(() => {
438
438
  this.SUBRULE(this.question);
439
439
  });
440
- this.MANY2(() => {
441
- this.SUBRULE(this.contentBlock);
440
+ this.MANY2({
441
+ GATE: this.BACKTRACK(this.contentBlock),
442
+ DEF: () => this.SUBRULE(this.contentBlock)
442
443
  });
443
444
  this.MANY3(() => {
444
445
  this.SUBRULE(this.tool);
445
446
  });
446
- this.SUBRULE(this.validation);
447
+ this.OPTION2(() => {
448
+ this.SUBRULE(this.validation);
449
+ });
447
450
  this.MANY4(() => {
448
451
  this.OR2([
449
452
  { ALT: () => this.CONSUME2(Newline) },
@@ -664,7 +667,7 @@ class MathExerciseParser extends CstParser {
664
667
  this.MANY2(() => {
665
668
  this.CONSUME2(Space);
666
669
  });
667
- this.AT_LEAST_ONE2(() => {
670
+ this.MANY3(() => {
668
671
  this.CONSUME(Newline);
669
672
  });
670
673
  });
@@ -1201,7 +1204,7 @@ const ExerciseDocumentSchema = z.object({
1201
1204
  question: TextBlockSchema.optional(),
1202
1205
  lines: z.array(ContentBlockSchema),
1203
1206
  tools: z.array(ToolNodeSchema).optional(),
1204
- validation: ValidationNodeSchema
1207
+ validation: ValidationNodeSchema.optional()
1205
1208
  });
1206
1209
  function createExerciseDocument(question, lines, validation, tools) {
1207
1210
  return ExerciseDocumentSchema.parse({
@@ -1228,7 +1231,7 @@ class NaturalDSLVisitor extends BaseCstVisitor {
1228
1231
  const question = ctx.question ? this.visit(ctx.question) : void 0;
1229
1232
  const contentBlocks = ctx.contentBlock ? ctx.contentBlock.map((cb) => this.visit(cb)) : [];
1230
1233
  const tools = ctx.tool ? ctx.tool.map((t) => this.visit(t)) : [];
1231
- const validation = this.visit(ctx.validation);
1234
+ const validation = ctx.validation ? this.visit(ctx.validation) : void 0;
1232
1235
  return createExerciseDocument(question, contentBlocks, validation, tools);
1233
1236
  }
1234
1237
  // ═══════════════════════════════════════════════════════════════════════════
@@ -1693,7 +1696,7 @@ const ExerciseJSONSchema = z.object({
1693
1696
  id: z.string().optional(),
1694
1697
  layout: z.array(LayoutItemSchema),
1695
1698
  widgets: z.record(WidgetJSONSchema),
1696
- validation: ValidationJSONSchema,
1699
+ validation: ValidationJSONSchema.optional(),
1697
1700
  meta: ExerciseMetaSchema.optional()
1698
1701
  });
1699
1702
  function validateExerciseJSON(data) {
@@ -1850,13 +1853,12 @@ function toJSON(ast) {
1850
1853
  }
1851
1854
  }
1852
1855
  }
1853
- const validation = transformValidation(ast.validation, answers, widgets);
1854
1856
  return {
1855
1857
  version: "2.0",
1856
1858
  // Natural DSL version
1857
1859
  layout,
1858
1860
  widgets,
1859
- validation
1861
+ ...ast.validation ? { validation: transformValidation(ast.validation, answers, widgets) } : {}
1860
1862
  };
1861
1863
  }
1862
1864
  function collectAllInlines(blocks) {
@@ -2327,11 +2329,18 @@ function findValidCombination(expression, ranges) {
2327
2329
  return null;
2328
2330
  }
2329
2331
  function validate(exercise, userInputs) {
2330
- switch (exercise.validation.mode) {
2332
+ if (!exercise.validation) {
2333
+ return {
2334
+ isCorrect: true,
2335
+ details: {}
2336
+ };
2337
+ }
2338
+ const validatedExercise = exercise;
2339
+ switch (validatedExercise.validation.mode) {
2331
2340
  case VALIDATION_MODES.SIMPLE:
2332
- return validateSimple(exercise, userInputs);
2341
+ return validateSimple(validatedExercise, userInputs);
2333
2342
  case VALIDATION_MODES.EXPRESSION:
2334
- return validateExpression(exercise, userInputs);
2343
+ return validateExpression(validatedExercise, userInputs);
2335
2344
  default:
2336
2345
  return {
2337
2346
  isCorrect: false,
@@ -2342,6 +2351,9 @@ function validate(exercise, userInputs) {
2342
2351
  }
2343
2352
  function getMissingInputs(exercise, userInputs) {
2344
2353
  const missing = [];
2354
+ if (!exercise.validation) {
2355
+ return missing;
2356
+ }
2345
2357
  if (exercise.validation.mode === VALIDATION_MODES.SIMPLE && exercise.validation.answers) {
2346
2358
  for (const widgetId of Object.keys(exercise.validation.answers)) {
2347
2359
  if (!(widgetId in userInputs) || userInputs[widgetId] === "") {