Skip to content

Commit 0384cf9

Browse files
authored
Reduce loglevel on node def validation (#295)
* Lower the loglevel of node def validation messages * Fix tests
1 parent 4bebcb4 commit 0384cf9

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

src/scripts/api.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,17 @@ class ComfyApi extends EventTarget {
249249
const objectInfoUnsafe = await resp.json()
250250
const objectInfo: Record<string, ComfyNodeDef> = {}
251251
for (const key in objectInfoUnsafe) {
252-
try {
253-
objectInfo[key] = validateComfyNodeDef(objectInfoUnsafe[key])
254-
} catch (e) {
255-
console.warn('Ignore node definition: ', key)
256-
console.error(e)
252+
const validatedDef = await validateComfyNodeDef(
253+
objectInfoUnsafe[key],
254+
/* onError=*/ (errorMessage: string) => {
255+
console.warn(
256+
`Skipping invalid node definition: ${key}. See debug log for more information.`
257+
)
258+
console.debug(errorMessage)
259+
}
260+
)
261+
if (validatedDef !== null) {
262+
objectInfo[key] = validatedDef
257263
}
258264
}
259265
return objectInfo

src/types/apiTypes.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,17 @@ export type ComfyInputsSpec = z.infer<typeof zComfyInputsSpec>
285285
export type ComfyOutputTypesSpec = z.infer<typeof zComfyOutputTypesSpec>
286286
export type ComfyNodeDef = z.infer<typeof zComfyNodeDef>
287287

288-
export function validateComfyNodeDef(data: any): ComfyNodeDef {
289-
const result = zComfyNodeDef.safeParse(data)
288+
export async function validateComfyNodeDef(
289+
data: any,
290+
onError: (error: string) => void = console.warn
291+
): Promise<ComfyNodeDef | null> {
292+
const result = await zComfyNodeDef.safeParseAsync(data)
290293
if (!result.success) {
291294
const zodError = fromZodError(result.error)
292-
const error = new Error(
295+
onError(
293296
`Invalid ComfyNodeDef: ${JSON.stringify(data)}\n${zodError.message}`
294297
)
295-
error.cause = zodError
296-
throw error
298+
return null
297299
}
298300
return result.data
299301
}

tests-ui/tests/apiTypes.test.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ const EXAMPLE_NODE_DEF: ComfyNodeDef = {
2020
}
2121

2222
describe('validateNodeDef', () => {
23-
it('Should accept a valid node definition', () => {
24-
expect(() => validateComfyNodeDef(EXAMPLE_NODE_DEF)).not.toThrow()
23+
it('Should accept a valid node definition', async () => {
24+
expect(await validateComfyNodeDef(EXAMPLE_NODE_DEF)).not.toBeNull()
2525
})
2626

2727
describe.each([
@@ -35,14 +35,16 @@ describe('validateNodeDef', () => {
3535
])(
3636
'validateComfyNodeDef with various input spec formats',
3737
(inputSpec, expected) => {
38-
it(`should accept input spec format: ${JSON.stringify(inputSpec)}`, () => {
38+
it(`should accept input spec format: ${JSON.stringify(inputSpec)}`, async () => {
3939
expect(
40-
validateComfyNodeDef({
41-
...EXAMPLE_NODE_DEF,
42-
input: {
43-
required: inputSpec
44-
}
45-
}).input.required.ckpt_name
40+
(
41+
await validateComfyNodeDef({
42+
...EXAMPLE_NODE_DEF,
43+
input: {
44+
required: inputSpec
45+
}
46+
})
47+
).input.required.ckpt_name
4648
).toEqual(expected)
4749
})
4850
}
@@ -57,15 +59,15 @@ describe('validateNodeDef', () => {
5759
])(
5860
'validateComfyNodeDef rejects with various input spec formats',
5961
(inputSpec) => {
60-
it(`should accept input spec format: ${JSON.stringify(inputSpec)}`, () => {
61-
expect(() =>
62-
validateComfyNodeDef({
62+
it(`should accept input spec format: ${JSON.stringify(inputSpec)}`, async () => {
63+
expect(
64+
await validateComfyNodeDef({
6365
...EXAMPLE_NODE_DEF,
6466
input: {
6567
required: inputSpec
6668
}
6769
})
68-
).toThrow()
70+
).toBeNull()
6971
})
7072
}
7173
)
@@ -76,8 +78,9 @@ describe('validateNodeDef', () => {
7678
fs.readFileSync(path.resolve('./tests-ui/data/object_info.json'))
7779
)
7880
)
79-
nodeDefs.forEach((nodeDef) => {
80-
expect(() => validateComfyNodeDef(nodeDef)).not.toThrow()
81-
})
81+
82+
for (const nodeDef of nodeDefs) {
83+
expect(await validateComfyNodeDef(nodeDef)).not.toBeNull()
84+
}
8285
})
8386
})

0 commit comments

Comments
 (0)