Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce loglevel on node def validation #295

Merged
merged 2 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/scripts/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,17 @@ class ComfyApi extends EventTarget {
const objectInfoUnsafe = await resp.json()
const objectInfo: Record<string, ComfyNodeDef> = {}
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
Expand Down
12 changes: 7 additions & 5 deletions src/types/apiTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,17 @@ export type ComfyInputsSpec = z.infer<typeof zComfyInputsSpec>
export type ComfyOutputTypesSpec = z.infer<typeof zComfyOutputTypesSpec>
export type ComfyNodeDef = z.infer<typeof zComfyNodeDef>

export function validateComfyNodeDef(data: any): ComfyNodeDef {
const result = zComfyNodeDef.safeParse(data)
export async function validateComfyNodeDef(
data: any,
onError: (error: string) => void = console.warn
): Promise<ComfyNodeDef | null> {
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
}
35 changes: 19 additions & 16 deletions tests-ui/tests/apiTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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)
})
}
Expand All @@ -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()
})
}
)
Expand All @@ -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()
}
})
})
Loading