{"record":{"id":"c04e48991a5e67f4","repo":"toon-format/toon","slug":"invalid-array-length-seg-expected-non-negat","errorCode":null,"errorMessage":"Invalid array length: \"${seg}\" (expected non-negative integer with no leading zeros)","messagePattern":"Invalid array length: \"(.+?)\" \\(expected non-negative integer with no leading zeros\\)","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"packages/toon/src/decode/parser.ts","lineNumber":204,"sourceCode":"  if (content.endsWith(TAB)) {\n    delimiter = DELIMITERS.tab\n    content = content.slice(0, -1)\n  }\n  else if (content.endsWith(PIPE)) {\n    delimiter = DELIMITERS.pipe\n    content = content.slice(0, -1)\n  }\n\n  // Only a colon between the length and the optional delimiter symbol marks a keyed\n  // header; any other placement leaves a token that fails the length check below.\n  let keyed = false\n  if (content.endsWith(COLON)) {\n    keyed = true\n    content = content.slice(0, -1)\n  }\n\n  if (!BRACKET_LENGTH_PATTERN.test(content)) {\n    throw new SyntaxError(`Invalid array length: \"${seg}\" (expected non-negative integer with no leading zeros)`)\n  }\n\n  return { length: Number.parseInt(content, 10), delimiter, keyed }\n}\n\n/**\n * Parses the content of a field list into field entries, recursively\n * descending into nested field groups (`field{sub1,sub2}`).\n *\n * @remarks\n * Throws on empty segments, empty names, unmatched braces, and content\n * after a nested group's closing brace; callers decide strict fallthrough.\n */\nexport function parseFieldEntries(fieldsContent: string, delimiter: Delimiter): FieldNode[] {\n  const entries = splitFieldEntries(fieldsContent, delimiter)\n\n  return entries.map((entry) => {\n    const trimmedEntry = trimSpaces(entry)","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/toon-format/toon/blob/604eac266e35166bed6f5b9e3bd586e9c60a9330/packages/toon/src/decode/parser.ts#L186-L222","documentation":"While parsing a TOON array header bracket segment like `[3]` or `[2,]`, the length part failed the BRACKET_LENGTH_PATTERN check (must be a non-negative integer with no leading zeros). This catches malformed headers such as `[03]`, `[-1]`, `[abc]`, or `[1.5]` so the decoder fails fast instead of mis-sizing the array.","triggerScenarios":"Calling toon.decode (which reaches parseBracketSegment via parseArrayHeaderLine) on input whose array header has an invalid length — leading zeros (`[03 items,]`), negative or non-integer lengths, or non-numeric content between the brackets.","commonSituations":"Hand-edited TOON files with adjusted array sizes written as `05`; generators that zero-pad lengths; copy-paste corruption turning `[4]` into `[-4]` or `[4x]`; writing array headers by hand without knowing the strict integer format.","solutions":["Fix the array header to use a plain non-negative integer with no leading zeros, e.g. `[3]` not `[03]`","Remove any negative signs, decimals, or extra characters inside the brackets","If the length is dynamic, format it with String(n) rather than padded formatting","Validate generated TOON output against the spec/test fixtures before persisting"],"exampleFix":"// before\nitems[03]{a,b}:\n// after\nitems[3]{a,b}:","handlingStrategy":"try-catch","validationCode":"function hasValidArrayLengths(toonText) {\n  return !/\\[\\s*(?:-|0\\d|[^0-9\\s])[0-9.,;\\t]*\\s*\\]/.test(toonText)\n}","typeGuard":null,"tryCatchPattern":"try {\n  const value = toon.decode(text)\n} catch (err) {\n  if (err instanceof SyntaxError && err.message.startsWith('Invalid array length')) {\n    // surface header line / column to user for correction\n  } else throw err\n}","preventionTips":["Write array lengths as plain integers with no leading zeros","Never hand-edit array lengths; re-encode with toon.encode after data changes","Format dynamic lengths with String(n), not padded formatters","Round-trip generated TOON through decode in tests"],"tags":["syntax","toon","array-header","parser"],"backgroundTag":"invalid-array-header","analyzedSha":"604eac266e35166bed6f5b9e3bd586e9c60a9330","analyzedAt":"2026-08-31T11:09:25.043Z","schemaVersion":2},"datasetVersion":"2026-09-01T08:17:40.651Z"}