toon-format/toon · error · SyntaxError
Invalid array length: "${seg}" (expected non-negative intege
Error message
Invalid array length: "${seg}" (expected non-negative integer with no leading zeros) What it means
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.
Source
Thrown at packages/toon/src/decode/parser.ts:204
if (content.endsWith(TAB)) {
delimiter = DELIMITERS.tab
content = content.slice(0, -1)
}
else if (content.endsWith(PIPE)) {
delimiter = DELIMITERS.pipe
content = content.slice(0, -1)
}
// Only a colon between the length and the optional delimiter symbol marks a keyed
// header; any other placement leaves a token that fails the length check below.
let keyed = false
if (content.endsWith(COLON)) {
keyed = true
content = content.slice(0, -1)
}
if (!BRACKET_LENGTH_PATTERN.test(content)) {
throw new SyntaxError(`Invalid array length: "${seg}" (expected non-negative integer with no leading zeros)`)
}
return { length: Number.parseInt(content, 10), delimiter, keyed }
}
/**
* Parses the content of a field list into field entries, recursively
* descending into nested field groups (`field{sub1,sub2}`).
*
* @remarks
* Throws on empty segments, empty names, unmatched braces, and content
* after a nested group's closing brace; callers decide strict fallthrough.
*/
export function parseFieldEntries(fieldsContent: string, delimiter: Delimiter): FieldNode[] {
const entries = splitFieldEntries(fieldsContent, delimiter)
return entries.map((entry) => {
const trimmedEntry = trimSpaces(entry)View on GitHub (pinned to 604eac266e)
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
Example fix
// before
items[03]{a,b}:
// after
items[3]{a,b}: Defensive patterns
Strategy: try-catch
Validate before calling
function hasValidArrayLengths(toonText) {
return !/\[\s*(?:-|0\d|[^0-9\s])[0-9.,;\t]*\s*\]/.test(toonText)
} Try / catch
try {
const value = toon.decode(text)
} catch (err) {
if (err instanceof SyntaxError && err.message.startsWith('Invalid array length')) {
// surface header line / column to user for correction
} else throw err
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- result.reason
- Empty field name in field list
- Missing field name before nested field group
- Unmatched brace in field list
- Unexpected content after nested field group
AI-assisted analysis of toon-format/toon@604eac266e (2026-08-31).
Data as JSON: /api/errors/c04e48991a5e67f4.
Report an issue: GitHub.