@@ -5,9 +5,14 @@ import type { WorkflowState } from '../workflow/types'
55const logger = createLogger ( 'WorkflowJsonImporter' )
66
77/**
8- * Normalize subblock values by converting empty strings to null.
8+ * Normalize subblock values by converting empty strings to null and filtering out invalid subblocks .
99 * This provides backwards compatibility for workflows exported before the null sanitization fix,
1010 * preventing Zod validation errors like "Expected array, received string".
11+ *
12+ * Also filters out malformed subBlocks that may have been created by bugs in previous exports:
13+ * - SubBlocks with key "undefined" (caused by assigning to undefined key)
14+ * - SubBlocks missing required fields like `id`
15+ * - SubBlocks with `type: "unknown"` (indicates malformed data)
1116 */
1217function normalizeSubblockValues ( blocks : Record < string , any > ) : Record < string , any > {
1318 const normalizedBlocks : Record < string , any > = { }
@@ -19,6 +24,34 @@ function normalizeSubblockValues(blocks: Record<string, any>): Record<string, an
1924 const normalizedSubBlocks : Record < string , any > = { }
2025
2126 Object . entries ( block . subBlocks ) . forEach ( ( [ subBlockId , subBlock ] : [ string , any ] ) => {
27+ // Skip subBlocks with invalid keys (literal "undefined" string)
28+ if ( subBlockId === 'undefined' ) {
29+ logger . warn ( `Skipping malformed subBlock with key "undefined" in block ${ blockId } ` )
30+ return
31+ }
32+
33+ // Skip subBlocks that are null or not objects
34+ if ( ! subBlock || typeof subBlock !== 'object' ) {
35+ logger . warn ( `Skipping invalid subBlock ${ subBlockId } in block ${ blockId } : not an object` )
36+ return
37+ }
38+
39+ // Skip subBlocks with type "unknown" (malformed data)
40+ if ( subBlock . type === 'unknown' ) {
41+ logger . warn (
42+ `Skipping malformed subBlock ${ subBlockId } in block ${ blockId } : type is "unknown"`
43+ )
44+ return
45+ }
46+
47+ // Skip subBlocks missing required id field
48+ if ( ! subBlock . id ) {
49+ logger . warn (
50+ `Skipping malformed subBlock ${ subBlockId } in block ${ blockId } : missing id field`
51+ )
52+ return
53+ }
54+
2255 const normalizedSubBlock = { ...subBlock }
2356
2457 // Convert empty strings to null for consistency
0 commit comments