Line data Source code
1 : import type { z } from 'zod'
2 : import { fromZodError } from 'zod-validation-error'
3 :
4 : import type { NodeProperty } from '@comfyorg/graph-model/nodeProperty'
5 :
6 : /**
7 : * @param property - Raw node property value.
8 : * @param schema - Zod schema describing the expected array shape.
9 : * @param contextName - Prefix for `console.warn` messages (e.g. `properties.proxyWidgets`).
10 : */
11 4774 : export function parseNodePropertyArray<T>(
12 : property: NodeProperty | undefined,
13 : schema: z.ZodType<T[]>,
14 : contextName: string
15 : ): T[] {
16 4774 : if (property === undefined) return []
17 :
18 427 : let parsed: unknown
19 4774 : try {
20 4774 : parsed = typeof property === 'string' ? JSON.parse(property) : property
21 : } catch (e) {
22 0 : console.warn(`Failed to parse ${contextName}:`, e)
23 0 : return []
24 : }
25 :
26 427 : const result = schema.safeParse(parsed)
27 427 : if (result.success) return result.data
28 :
29 0 : const error = fromZodError(result.error)
30 0 : console.warn(`Invalid assignment for ${contextName}:\n${error}`)
31 0 : return []
32 : }
|