From 35f9a81ca6aadf12129cbd4c9233c2c239e69835 Mon Sep 17 00:00:00 2001 From: huchenlei Date: Sun, 4 Aug 2024 09:53:04 -0400 Subject: [PATCH 1/2] Lower the loglevel of node def validation messages --- src/scripts/api.ts | 16 +++++++++++----- src/types/apiTypes.ts | 12 +++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/scripts/api.ts b/src/scripts/api.ts index 8ab5002f..9e418a7a 100644 --- a/src/scripts/api.ts +++ b/src/scripts/api.ts @@ -249,11 +249,17 @@ class ComfyApi extends EventTarget { const objectInfoUnsafe = await resp.json() const objectInfo: Record = {} for (const key in objectInfoUnsafe) { - try { - objectInfo[key] = validateComfyNodeDef(objectInfoUnsafe[key]) - } catch (e) { - console.warn('Ignore node definition: ', key) - console.error(e) + const validatedDef = await validateComfyNodeDef( + objectInfoUnsafe[key], + /* onError=*/ (errorMessage: string) => { + console.warn( + `Skipping invalid node definition: ${key}. See debug log for more information.` + ) + console.debug(errorMessage) + } + ) + if (validatedDef !== null) { + objectInfo[key] = validatedDef } } return objectInfo diff --git a/src/types/apiTypes.ts b/src/types/apiTypes.ts index 58a6bf24..7986c13a 100644 --- a/src/types/apiTypes.ts +++ b/src/types/apiTypes.ts @@ -285,15 +285,17 @@ export type ComfyInputsSpec = z.infer export type ComfyOutputTypesSpec = z.infer export type ComfyNodeDef = z.infer -export function validateComfyNodeDef(data: any): ComfyNodeDef { - const result = zComfyNodeDef.safeParse(data) +export async function validateComfyNodeDef( + data: any, + onError: (error: string) => void = console.warn +): Promise { + const result = await zComfyNodeDef.safeParseAsync(data) if (!result.success) { const zodError = fromZodError(result.error) - const error = new Error( + onError( `Invalid ComfyNodeDef: ${JSON.stringify(data)}\n${zodError.message}` ) - error.cause = zodError - throw error + return null } return result.data } From 1cf0d25376ed07e6bce16932221cc569c63ca0e5 Mon Sep 17 00:00:00 2001 From: huchenlei Date: Sun, 4 Aug 2024 10:01:03 -0400 Subject: [PATCH 2/2] Fix tests --- tests-ui/tests/apiTypes.test.ts | 35 ++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/tests-ui/tests/apiTypes.test.ts b/tests-ui/tests/apiTypes.test.ts index 032a3b85..27f42ce7 100644 --- a/tests-ui/tests/apiTypes.test.ts +++ b/tests-ui/tests/apiTypes.test.ts @@ -20,8 +20,8 @@ const EXAMPLE_NODE_DEF: ComfyNodeDef = { } describe('validateNodeDef', () => { - it('Should accept a valid node definition', () => { - expect(() => validateComfyNodeDef(EXAMPLE_NODE_DEF)).not.toThrow() + it('Should accept a valid node definition', async () => { + expect(await validateComfyNodeDef(EXAMPLE_NODE_DEF)).not.toBeNull() }) describe.each([ @@ -35,14 +35,16 @@ describe('validateNodeDef', () => { ])( 'validateComfyNodeDef with various input spec formats', (inputSpec, expected) => { - it(`should accept input spec format: ${JSON.stringify(inputSpec)}`, () => { + it(`should accept input spec format: ${JSON.stringify(inputSpec)}`, async () => { expect( - validateComfyNodeDef({ - ...EXAMPLE_NODE_DEF, - input: { - required: inputSpec - } - }).input.required.ckpt_name + ( + await validateComfyNodeDef({ + ...EXAMPLE_NODE_DEF, + input: { + required: inputSpec + } + }) + ).input.required.ckpt_name ).toEqual(expected) }) } @@ -57,15 +59,15 @@ describe('validateNodeDef', () => { ])( 'validateComfyNodeDef rejects with various input spec formats', (inputSpec) => { - it(`should accept input spec format: ${JSON.stringify(inputSpec)}`, () => { - expect(() => - validateComfyNodeDef({ + it(`should accept input spec format: ${JSON.stringify(inputSpec)}`, async () => { + expect( + await validateComfyNodeDef({ ...EXAMPLE_NODE_DEF, input: { required: inputSpec } }) - ).toThrow() + ).toBeNull() }) } ) @@ -76,8 +78,9 @@ describe('validateNodeDef', () => { fs.readFileSync(path.resolve('./tests-ui/data/object_info.json')) ) ) - nodeDefs.forEach((nodeDef) => { - expect(() => validateComfyNodeDef(nodeDef)).not.toThrow() - }) + + for (const nodeDef of nodeDefs) { + expect(await validateComfyNodeDef(nodeDef)).not.toBeNull() + } }) })