@mathscan/math-exercise-engine 1.0.9 → 1.0.10

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
@@ -443,7 +443,9 @@ class MathExerciseParser extends CstParser {
443
443
  this.MANY3(() => {
444
444
  this.SUBRULE(this.tool);
445
445
  });
446
- this.SUBRULE(this.validation);
446
+ this.OPTION2(() => {
447
+ this.SUBRULE(this.validation);
448
+ });
447
449
  this.MANY4(() => {
448
450
  this.OR2([
449
451
  { ALT: () => this.CONSUME2(Newline) },
@@ -1201,7 +1203,7 @@ const ExerciseDocumentSchema = z.object({
1201
1203
  question: TextBlockSchema.optional(),
1202
1204
  lines: z.array(ContentBlockSchema),
1203
1205
  tools: z.array(ToolNodeSchema).optional(),
1204
- validation: ValidationNodeSchema
1206
+ validation: ValidationNodeSchema.optional()
1205
1207
  });
1206
1208
  function createExerciseDocument(question, lines, validation, tools) {
1207
1209
  return ExerciseDocumentSchema.parse({
@@ -1228,7 +1230,7 @@ class NaturalDSLVisitor extends BaseCstVisitor {
1228
1230
  const question = ctx.question ? this.visit(ctx.question) : void 0;
1229
1231
  const contentBlocks = ctx.contentBlock ? ctx.contentBlock.map((cb) => this.visit(cb)) : [];
1230
1232
  const tools = ctx.tool ? ctx.tool.map((t) => this.visit(t)) : [];
1231
- const validation = this.visit(ctx.validation);
1233
+ const validation = ctx.validation ? this.visit(ctx.validation) : void 0;
1232
1234
  return createExerciseDocument(question, contentBlocks, validation, tools);
1233
1235
  }
1234
1236
  // ═══════════════════════════════════════════════════════════════════════════
@@ -1693,7 +1695,7 @@ const ExerciseJSONSchema = z.object({
1693
1695
  id: z.string().optional(),
1694
1696
  layout: z.array(LayoutItemSchema),
1695
1697
  widgets: z.record(WidgetJSONSchema),
1696
- validation: ValidationJSONSchema,
1698
+ validation: ValidationJSONSchema.optional(),
1697
1699
  meta: ExerciseMetaSchema.optional()
1698
1700
  });
1699
1701
  function validateExerciseJSON(data) {
@@ -1850,13 +1852,12 @@ function toJSON(ast) {
1850
1852
  }
1851
1853
  }
1852
1854
  }
1853
- const validation = transformValidation(ast.validation, answers, widgets);
1854
1855
  return {
1855
1856
  version: "2.0",
1856
1857
  // Natural DSL version
1857
1858
  layout,
1858
1859
  widgets,
1859
- validation
1860
+ ...ast.validation ? { validation: transformValidation(ast.validation, answers, widgets) } : {}
1860
1861
  };
1861
1862
  }
1862
1863
  function collectAllInlines(blocks) {
@@ -2327,11 +2328,18 @@ function findValidCombination(expression, ranges) {
2327
2328
  return null;
2328
2329
  }
2329
2330
  function validate(exercise, userInputs) {
2330
- switch (exercise.validation.mode) {
2331
+ if (!exercise.validation) {
2332
+ return {
2333
+ isCorrect: true,
2334
+ details: {}
2335
+ };
2336
+ }
2337
+ const validatedExercise = exercise;
2338
+ switch (validatedExercise.validation.mode) {
2331
2339
  case VALIDATION_MODES.SIMPLE:
2332
- return validateSimple(exercise, userInputs);
2340
+ return validateSimple(validatedExercise, userInputs);
2333
2341
  case VALIDATION_MODES.EXPRESSION:
2334
- return validateExpression(exercise, userInputs);
2342
+ return validateExpression(validatedExercise, userInputs);
2335
2343
  default:
2336
2344
  return {
2337
2345
  isCorrect: false,
@@ -2342,6 +2350,9 @@ function validate(exercise, userInputs) {
2342
2350
  }
2343
2351
  function getMissingInputs(exercise, userInputs) {
2344
2352
  const missing = [];
2353
+ if (!exercise.validation) {
2354
+ return missing;
2355
+ }
2345
2356
  if (exercise.validation.mode === VALIDATION_MODES.SIMPLE && exercise.validation.answers) {
2346
2357
  for (const widgetId of Object.keys(exercise.validation.answers)) {
2347
2358
  if (!(widgetId in userInputs) || userInputs[widgetId] === "") {