[Nope] Zod schema recursive II

This commit is contained in:
filtered
2025-04-29 03:13:04 +10:00
parent b3042d346a
commit 9620f833aa

View File

@@ -41,10 +41,10 @@ const zModelFile = z.object({
const zGraphState = z const zGraphState = z
.object({ .object({
lastGroupid: z.number().optional(), lastGroupId: z.number(),
lastNodeId: z.number().optional(), lastNodeId: z.number(),
lastLinkId: z.number().optional(), lastLinkId: z.number(),
lastRerouteId: z.number().optional() lastRerouteId: z.number()
}) })
.passthrough() .passthrough()
@@ -214,6 +214,15 @@ const zComfyNode = z
}) })
.passthrough() .passthrough()
export const zSubgraphIO = zNodeInput.extend({
/** Slot ID (internal; never changes once instantiated). */
id: z.string().uuid(),
/** The data type this slot uses. Unlike nodes, this does not support legacy numeric types. */
type: z.string(),
/** Links connected to this slot, or `undefined` if not connected. An ouptut slot should only ever have one link. */
linkIds: z.array(z.number()).optional()
})
const zSubgraphInstance = z const zSubgraphInstance = z
.object({ .object({
id: zNodeId, id: zNodeId,
@@ -223,8 +232,8 @@ const zSubgraphInstance = z
flags: zFlags, flags: zFlags,
order: z.number(), order: z.number(),
mode: z.number(), mode: z.number(),
inputs: z.array(zNodeInput).optional(), inputs: z.array(zSubgraphIO).optional(),
outputs: z.array(zNodeOutput).optional(), outputs: z.array(zSubgraphIO).optional(),
widgets_values: zWidgetValues.optional(), widgets_values: zWidgetValues.optional(),
color: z.string().optional(), color: z.string().optional(),
bgcolor: z.string().optional() bgcolor: z.string().optional()
@@ -303,39 +312,41 @@ interface ComfyWorkflow1BaseType {
revision?: number revision?: number
version: 1 version: 1
models?: z.infer<typeof zModelFile>[] models?: z.infer<typeof zModelFile>[]
definitions?: { state: z.infer<typeof zGraphState>
subgraphs: SubgraphInferredBaseType[]
}
} }
/** Required for recursive definition of subgraphs. */ /** Required for recursive definition of subgraphs. */
interface ComfyWorkflow1BaseInput extends ComfyWorkflow1BaseType { interface ComfyWorkflow1BaseInput extends ComfyWorkflow1BaseType {
state: z.input<typeof zGraphState>
groups?: z.input<typeof zGroup>[] groups?: z.input<typeof zGroup>[]
nodes: z.input<typeof zComfyNode>[] nodes: z.input<typeof zComfyNode>[]
links?: z.input<typeof zComfyLinkObject>[] links?: z.input<typeof zComfyLinkObject>[]
floatingLinks?: z.input<typeof zComfyLinkObject>[] floatingLinks?: z.input<typeof zComfyLinkObject>[]
reroutes?: z.input<typeof zReroute>[] reroutes?: z.input<typeof zReroute>[]
definitions?: {
subgraphs: SubgraphDefinitionBaseInput[]
}
} }
/** Required for recursive definition of subgraphs. */ /** Required for recursive definition of subgraphs. */
interface ComfyWorkflow1BaseOutput extends ComfyWorkflow1BaseType { interface ComfyWorkflow1BaseOutput extends ComfyWorkflow1BaseType {
state: z.output<typeof zGraphState>
groups?: z.output<typeof zGroup>[] groups?: z.output<typeof zGroup>[]
nodes: z.output<typeof zComfyNode>[] nodes: z.output<typeof zComfyNode>[]
links?: z.output<typeof zComfyLinkObject>[] links?: z.output<typeof zComfyLinkObject>[]
floatingLinks?: z.output<typeof zComfyLinkObject>[] floatingLinks?: z.output<typeof zComfyLinkObject>[]
reroutes?: z.output<typeof zReroute>[] reroutes?: z.output<typeof zReroute>[]
definitions?: {
subgraphs: SubgraphDefinitionBaseOutput[]
}
} }
/** Required for recursive definition of subgraphs. */ /** Required for recursive definition of subgraphs. */
type ComfyWorkflow1DefinitionInput = z.input<typeof zBaseExportableGraph> & type SubgraphDefinitionInput = z.input<typeof zBaseExportableGraph> &
ComfyWorkflow1BaseInput & ComfyWorkflow1BaseInput &
SubgraphInferredBaseType SubgraphDefinitionBaseInput
/** Required for recursive definition of subgraphs. */ /** Required for recursive definition of subgraphs. */
type ComfyWorkflow1DefinitionOutput = z.output<typeof zBaseExportableGraph> & type SubgraphDefinitionOutput = z.output<typeof zBaseExportableGraph> &
ComfyWorkflow1BaseOutput & ComfyWorkflow1BaseOutput &
SubgraphInferredBaseType SubgraphDefinitionBaseOutput
/** Schema version 1 */ /** Schema version 1 */
export const zComfyWorkflow1 = zBaseExportableGraph export const zComfyWorkflow1 = zBaseExportableGraph
@@ -356,9 +367,9 @@ export const zComfyWorkflow1 = zBaseExportableGraph
subgraphs: z.lazy( subgraphs: z.lazy(
(): z.ZodArray< (): z.ZodArray<
z.ZodType< z.ZodType<
ComfyWorkflow1DefinitionOutput, SubgraphDefinitionOutput,
z.ZodTypeDef, z.ZodTypeDef,
ComfyWorkflow1DefinitionInput SubgraphDefinitionInput
>, >,
'many' 'many'
> => z.array(zSubgraphDefinition) > => z.array(zSubgraphDefinition)
@@ -367,15 +378,6 @@ export const zComfyWorkflow1 = zBaseExportableGraph
}) })
.passthrough() .passthrough()
export const zSubgraphIO = z.object({
/** Slot ID (internal; never changes once instantiated). */
id: z.string().uuid(),
/** The data type this slot uses. Unlike nodes, this does not support legacy numeric types. */
type: z.string(),
/** Links connected to this slot, or `undefined` if not connected. An ouptut slot should only ever have one link. */
linkIds: z.array(z.number()).nullable().optional()
})
export const zExportedSubgraphIONode = z.object({ export const zExportedSubgraphIONode = z.object({
id: zNodeId, id: zNodeId,
bounding: z.tuple([z.number(), z.number(), z.number(), z.number()]), bounding: z.tuple([z.number(), z.number(), z.number(), z.number()]),
@@ -387,29 +389,43 @@ export const zExposedWidget = z.object({
name: z.string() name: z.string()
}) })
interface SubgraphInferredBaseType { interface SubgraphDefinitionBaseInput extends ComfyWorkflow1BaseInput {
/** Unique graph ID. Automatically generated if not provided. */ /** Unique graph ID. Automatically generated if not provided. */
id: string id: string
revision: number revision: number
inputNode: z.infer<typeof zExportedSubgraphIONode> name: string
outputNode: z.infer<typeof zExportedSubgraphIONode>
inputNode: z.input<typeof zExportedSubgraphIONode>
outputNode: z.input<typeof zExportedSubgraphIONode>
/** Ordered list of inputs to the subgraph itself. Similar to a reroute, with the input side in the graph, and the output side in the subgraph. */ /** Ordered list of inputs to the subgraph itself. Similar to a reroute, with the input side in the graph, and the output side in the subgraph. */
inputs?: z.infer<typeof zSubgraphIO>[] inputs?: z.input<typeof zSubgraphIO>[]
/** Ordered list of outputs from the subgraph itself. Similar to a reroute, with the input side in the subgraph, and the output side in the graph. */ /** Ordered list of outputs from the subgraph itself. Similar to a reroute, with the input side in the subgraph, and the output side in the graph. */
outputs?: z.infer<typeof zSubgraphIO>[] outputs?: z.input<typeof zSubgraphIO>[]
/** A list of node widgets displayed in the parent graph, on the subgraph object. */ /** A list of node widgets displayed in the parent graph, on the subgraph object. */
widgets?: z.infer<typeof zExposedWidget>[] widgets?: z.input<typeof zExposedWidget>[]
definitions?: { definitions?: {
subgraphs: SubgraphInferredBaseType[] subgraphs: SubgraphDefinitionBaseInput[]
} }
} }
/** Required for recursive definition of subgraphs. */ interface SubgraphDefinitionBaseOutput extends ComfyWorkflow1BaseOutput {
type SubgraphDefinitionInput = z.input<typeof zComfyWorkflow1> & /** Unique graph ID. Automatically generated if not provided. */
SubgraphInferredBaseType id: string
/** Required for recursive definition of subgraphs. */ revision: number
type SubgraphDefinitionOutput = z.output<typeof zComfyWorkflow1> & name: string
SubgraphInferredBaseType
inputNode: z.output<typeof zExportedSubgraphIONode>
outputNode: z.output<typeof zExportedSubgraphIONode>
/** Ordered list of inputs to the subgraph itself. Similar to a reroute, with the input side in the graph, and the output side in the subgraph. */
inputs?: z.output<typeof zSubgraphIO>[]
/** Ordered list of outputs from the subgraph itself. Similar to a reroute, with the input side in the subgraph, and the output side in the graph. */
outputs?: z.output<typeof zSubgraphIO>[]
/** A list of node widgets displayed in the parent graph, on the subgraph object. */
widgets?: z.output<typeof zExposedWidget>[]
definitions?: {
subgraphs: SubgraphDefinitionBaseOutput[]
}
}
/** A subgraph definition `worfklow.definitions.subgraphs` */ /** A subgraph definition `worfklow.definitions.subgraphs` */
export const zSubgraphDefinition = zComfyWorkflow1 export const zSubgraphDefinition = zComfyWorkflow1
@@ -417,7 +433,7 @@ export const zSubgraphDefinition = zComfyWorkflow1
/** Unique graph ID. Automatically generated if not provided. */ /** Unique graph ID. Automatically generated if not provided. */
id: z.string().uuid(), id: z.string().uuid(),
revision: z.number(), revision: z.number(),
name: z.string(),
inputNode: zExportedSubgraphIONode, inputNode: zExportedSubgraphIONode,
outputNode: zExportedSubgraphIONode, outputNode: zExportedSubgraphIONode,