codex-team/editor.js · error · Error
Index should be greater than or equal to 0
Error message
Index should be greater than or equal to 0
What it means
validateIndex rejects negative indices for blocks.insertMany; an index below 0 is meaningless in the blocks array and throws immediately.
Source
Thrown at src/components/modules/api/blocks.ts:421
this.Editor.BlockManager.insertMany(blocksToInsert, index);
// we cast to any because our BlockAPI has no "new" signature
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return blocksToInsert.map((block) => new (BlockAPI as any)(block));
};
/**
* Validated block index and throws an error if it's invalid
*
* @param index - index to validate
*/
private validateIndex(index: unknown): void {
if (typeof index !== 'number') {
throw new Error('Index should be a number');
}
if (index < 0) {
throw new Error(`Index should be greater than or equal to 0`);
}
if (index === null) {
throw new Error(`Index should be greater than or equal to 0`);
}
}
}
View on GitHub (pinned to 5f45dabbe5)
Solutions
- Clamp the index: Math.max(0, index)
- Fix the off-by-one in the index computation
- Validate with a guard before calling insertMany
Example fix
// before editor.blocks.insertMany(newBlocks, currentIndex - 1); // after editor.blocks.insertMany(newBlocks, Math.max(0, currentIndex - 1));
Defensive patterns
Strategy: validation
Validate before calling
index = Math.max(0, index); editor.blocks.insertMany(blocks, index);
Type guard
const isNonNegativeInt = (i: unknown): i is number => Number.isInteger(i) && (i as number) >= 0;
Try / catch
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; } Prevention
- Clamp computed indices with Math.max(0, n)
- Watch idx - 1 patterns at the top of the document
When it happens
Trigger: Calling insertMany(blocks, -1) or passing a computed index that went negative (e.g. currentIndex - 1 when currentIndex is 0).
Common situations: Off-by-one arithmetic like idx - 1 at the top of the document; clamp logic missing before insertion; parsing '-1' sentinels from external data.
Related errors
- Index should be a number
- Unable to move Block down since it is already the last
- Unable to move Block up since it is already the first
- Incorrect index
- Incorrect data passed to the render() method
AI-assisted analysis of codex-team/editor.js@5f45dabbe5 (2026-08-27).
Data as JSON: /api/errors/767d8e97e73c3d7b.
Report an issue: GitHub.