{"record":{"id":"a6cd8bbaae2d9718","repo":"mermaid-js/mermaid","slug":"nodes-array-is-required-in-layout-data","errorCode":null,"errorMessage":"Nodes array is required in layout data","messagePattern":"Nodes array is required in layout data","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/mermaid-layout-tidy-tree/src/layout.ts","lineNumber":645,"sourceCode":"  });\n}\n\n/**\n * Validate layout data structure\n * @param data - The data to validate\n * @returns True if data is valid, throws error otherwise\n */\nexport function validateLayoutData(data: LayoutData): boolean {\n  if (!data) {\n    throw new Error('Layout data is required');\n  }\n\n  if (!data.config) {\n    throw new Error('Configuration is required in layout data');\n  }\n\n  if (!Array.isArray(data.nodes)) {\n    throw new Error('Nodes array is required in layout data');\n  }\n\n  if (!Array.isArray(data.edges)) {\n    throw new Error('Edges array is required in layout data');\n  }\n\n  return true;\n}\n","sourceCodeStart":627,"sourceCodeEnd":654,"githubUrl":"https://github.com/mermaid-js/mermaid/blob/d93e9c88c01a599c062ee6a3f1462e3558ac6b90/packages/mermaid-layout-tidy-tree/src/layout.ts#L627-L654","documentation":"Thrown by validateLayoutData when data.nodes is present but not an array. Unlike executeTidyTreeLayout's guard, this fires for type mismatches (object, string, number) even when the value is truthy, so it catches malformed shapes that an empty-length check alone would miss.","triggerScenarios":"Passing data.nodes as a Map, an object keyed by id, a single node object, or a serialized JSON string that was never parsed.","commonSituations":"A producer stored nodes in a record/object and forwarded it directly, or a JSON.parse boundary was skipped so nodes arrived as a string.","solutions":["Ensure data.nodes is always an Array<Node>; convert with Array.from(...) or Object.values(...) at the producer.","Run JSON.parse before validation if the payload came over a transport.","Add a TypeScript type annotation (nodes: Node[]) so the mismatch is caught at compile time."],"exampleFix":"// before\nvalidateLayoutData({ nodes: nodesMap, edges, config }); // Map\n\n// after\nvalidateLayoutData({ nodes: [...nodesMap.values()], edges, config });","handlingStrategy":"type-guard","validationCode":"if (!Array.isArray(data.nodes)) {\n  data.nodes = Object.values(data.nodes ?? {});\n}\nvalidateLayoutData(data);","typeGuard":"function hasNodeArray(data: unknown): data is { nodes: unknown[] } {\n  return Array.isArray((data as any).nodes);\n}","tryCatchPattern":null,"preventionTips":["Annotate nodes as Node[] everywhere to catch mismatches at compile time.","Run JSON.parse on transported payloads before validation."],"tags":["layout","validation","nodes","type-mismatch"],"backgroundTag":null,"analyzedSha":"d93e9c88c01a599c062ee6a3f1462e3558ac6b90","analyzedAt":"2026-08-12T06:23:11.304Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}