{"record":{"id":"bd42871cfa49f7eb","repo":"Yeachan-Heo/oh-my-codex","slug":"path-mismatchindex-must-match-the-selected","errorCode":null,"errorMessage":"${path}[${mismatchIndex}] must match the selected value label","messagePattern":"(.+?)\\[(.+?)\\] must match the selected value label","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/question/state.ts","lineNumber":335,"sourceCode":"  return selectedValues.map((value) => {\n    const optionLabel = optionLabelsByValue.get(value);\n    if (optionLabel) return optionLabel;\n    if (question.allow_other && otherText && value === otherText) return question.other_label;\n    throw new Error(`selected value is not in the option schema: ${value}`);\n  });\n}\n\nfunction assertSelectedLabelsMatch(\n  selectedLabels: string[],\n  expectedLabels: string[],\n  path: string,\n): void {\n  if (selectedLabels.length !== expectedLabels.length) {\n    throw new Error(`${path} cardinality must match selected values`);\n  }\n  const mismatchIndex = selectedLabels.findIndex((label, index) => label !== expectedLabels[index]);\n  if (mismatchIndex !== -1) {\n    throw new Error(`${path}[${mismatchIndex}] must match the selected value label`);\n  }\n}\n\nfunction questionForAnswer(record: QuestionRecord, entry: QuestionAnswerEntry): NormalizedQuestionItem {\n  const question = (record.questions ?? []).find((item) => item.id === entry.question_id);\n  if (question) return question;\n  if (entry.question_id === 'q-1') {\n    return {\n      id: 'q-1',\n      ...(record.header ? { header: record.header } : {}),\n      question: record.question,\n      options: record.options,\n      allow_other: record.allow_other,\n      other_label: record.other_label,\n      multi_select: record.multi_select,\n      type: getNormalizedQuestionType(record),\n    };\n  }","sourceCodeStart":317,"sourceCodeEnd":353,"githubUrl":"https://github.com/Yeachan-Heo/oh-my-codex/blob/3ad79a8a6fe6e95fdbb8c00e40716fffe4011ce2/src/question/state.ts#L317-L353","documentation":"assertSelectedLabelsMatch compares each selected_labels[i] against the label the schema expects for selected_values[i]. On the first index where they differ it throws with that index, pinpointing the misaligned label.","triggerScenarios":"selected_labels[i] does not equal question option label (or other_label for other answers) for the value at the same index; e.g. reordered values without reordering labels, or outdated label text after schema localization changes.","commonSituations":"Reordering one array but not the other; stale cached labels after the question's labels were updated; localization mismatch between client and server label text.","solutions":["Derive labels from the current question schema at submit time rather than caching them","When appending/removing a selection, update both arrays together (derive labels from values)"],"exampleFix":"// before\nanswer.selected_labels = cachedLabels; // stale/order mismatch\n// after\nanswer.selected_labels = answer.selected_values.map((v) => question.options.find((o) => o.value === v)!.label);","handlingStrategy":"validation","validationCode":"const expected = answer.selected_values.map((v) => question.options.find((o) => o.value === v)?.label ?? question.other_label);\nif (expected.some((l, i) => l !== answer.selected_labels[i])) answer.selected_labels = expected;","typeGuard":"function labelsMatchSchema(q: NormalizedQuestionItem, values: string[], labels: string[]): boolean {\n  return values.every((v, i) => labels[i] === (q.options.find((o) => o.value === v)?.label ?? q.other_label));\n}","tryCatchPattern":"try { await submit(p, answers); } catch (e) { const m = /answers\\[\\d+\\]\\.answer\\.selected_labels\\[(\\d+)\\] must match/.exec((e as Error).message); if (m) { fixLabelAt(+m[1]); return submit(p, answers); } throw e; }","preventionTips":["Compute labels at submit time from the live schema, not cached render state","Keep values and labels updated as one atomic pair"],"tags":["validation","labels","answers"],"backgroundTag":"schema-validation-failed","analyzedSha":"3ad79a8a6fe6e95fdbb8c00e40716fffe4011ce2","analyzedAt":"2026-08-27T22:18:39.783Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}