{"record":{"id":"767d8e97e73c3d7b","repo":"codex-team/editor.js","slug":"index-should-be-greater-than-or-equal-to-0","errorCode":null,"errorMessage":"Index should be greater than or equal to 0","messagePattern":"Index should be greater than or equal to 0","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/components/modules/api/blocks.ts","lineNumber":421,"sourceCode":"    this.Editor.BlockManager.insertMany(blocksToInsert, index);\n\n    // we cast to any because our BlockAPI has no \"new\" signature\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    return blocksToInsert.map((block) => new (BlockAPI as any)(block));\n  };\n\n  /**\n   * Validated block index and throws an error if it's invalid\n   *\n   * @param index - index to validate\n   */\n  private validateIndex(index: unknown): void {\n    if (typeof index !== 'number') {\n      throw new Error('Index should be a number');\n    }\n\n    if (index < 0) {\n      throw new Error(`Index should be greater than or equal to 0`);\n    }\n\n    if (index === null) {\n      throw new Error(`Index should be greater than or equal to 0`);\n    }\n  }\n}\n","sourceCodeStart":403,"sourceCodeEnd":429,"githubUrl":"https://github.com/codex-team/editor.js/blob/5f45dabbe5690b7033b2f9047d3985add5045fdd/src/components/modules/api/blocks.ts#L403-L429","documentation":"validateIndex rejects negative indices for blocks.insertMany; an index below 0 is meaningless in the blocks array and throws immediately.","triggerScenarios":"Calling insertMany(blocks, -1) or passing a computed index that went negative (e.g. currentIndex - 1 when currentIndex is 0).","commonSituations":"Off-by-one arithmetic like idx - 1 at the top of the document; clamp logic missing before insertion; parsing '-1' sentinels from external data.","solutions":["Clamp the index: Math.max(0, index)","Fix the off-by-one in the index computation","Validate with a guard before calling insertMany"],"exampleFix":"// before\neditor.blocks.insertMany(newBlocks, currentIndex - 1);\n// after\neditor.blocks.insertMany(newBlocks, Math.max(0, currentIndex - 1));","handlingStrategy":"validation","validationCode":"index = Math.max(0, index); editor.blocks.insertMany(blocks, index);","typeGuard":"const isNonNegativeInt = (i: unknown): i is number => Number.isInteger(i) && (i as number) >= 0;","tryCatchPattern":"try { editor.blocks.insertMany(blocks, index); } catch (e) { if (e instanceof Error && e.message.includes('greater than or equal to 0')) { editor.blocks.insertMany(blocks, 0); return; } throw e; }","preventionTips":["Clamp computed indices with Math.max(0, n)","Watch idx - 1 patterns at the top of the document"],"tags":["insert-many","index","bounds-check","editorjs"],"backgroundTag":"index-out-of-bounds","analyzedSha":"5f45dabbe5690b7033b2f9047d3985add5045fdd","analyzedAt":"2026-08-27T22:50:23.124Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}