11import { z } from 'zod'
22import { fromZodError } from 'zod-validation-error'
33
4+ // GroupNode is hacking node id to be a string, so we need to allow that.
5+ // innerNode.id = `${this.node.id}:${i}`
6+ // Remove it after GroupNode is redesigned.
7+ export const zNodeId = z . union ( [ z . number ( ) . int ( ) , z . string ( ) ] )
8+ export const zSlotIndex = z . union ( [
9+ z . number ( ) . int ( ) ,
10+ z
11+ . string ( )
12+ . transform ( ( val ) => parseInt ( val ) )
13+ . refine ( ( val ) => ! isNaN ( val ) , {
14+ message : 'Invalid number'
15+ } )
16+ ] )
17+
418// Definition of an AI model file used in the workflow.
519const zModelFile = z . object ( {
620 name : z . string ( ) ,
@@ -12,10 +26,10 @@ const zModelFile = z.object({
1226
1327const zComfyLink = z . tuple ( [
1428 z . number ( ) , // Link id
15- z . number ( ) , // Node id of source node
16- z . number ( ) , // Output slot# of source node
17- z . number ( ) , // Node id of destination node
18- z . number ( ) , // Input slot# of destination node
29+ zNodeId , // Node id of source node
30+ zSlotIndex , // Output slot# of source node
31+ zNodeId , // Node id of destination node
32+ zSlotIndex , // Input slot# of destination node
1933 z . string ( ) // Data type
2034] )
2135
@@ -24,7 +38,7 @@ const zNodeOutput = z
2438 name : z . string ( ) ,
2539 type : z . string ( ) ,
2640 links : z . array ( z . number ( ) ) . nullable ( ) ,
27- slot_index : z . number ( ) . optional ( )
41+ slot_index : zSlotIndex . optional ( )
2842 } )
2943 . passthrough ( )
3044
@@ -33,7 +47,7 @@ const zNodeInput = z
3347 name : z . string ( ) ,
3448 type : z . string ( ) ,
3549 link : z . number ( ) . nullable ( ) ,
36- slot_index : z . number ( ) . optional ( )
50+ slot_index : zSlotIndex . optional ( )
3751 } )
3852 . passthrough ( )
3953
@@ -62,7 +76,7 @@ const zWidgetValues = z.union([z.array(z.any()), z.record(z.any())])
6276
6377const zComfyNode = z
6478 . object ( {
65- id : z . number ( ) ,
79+ id : zNodeId ,
6680 type : z . string ( ) ,
6781 pos : zVector2 ,
6882 size : zVector2 ,
@@ -123,7 +137,7 @@ const zExtra = z
123137
124138export const zComfyWorkflow = z
125139 . object ( {
126- last_node_id : z . number ( ) ,
140+ last_node_id : zNodeId ,
127141 last_link_id : z . number ( ) ,
128142 nodes : z . array ( zComfyNode ) ,
129143 links : z . array ( zComfyLink ) ,
0 commit comments