From 1d02cd3c47e187365a9d9926414b6fea5627ead5 Mon Sep 17 00:00:00 2001 From: Chenlei Hu Date: Thu, 9 Jan 2025 11:30:47 -0500 Subject: [PATCH] [Docs] Extract comfy workflow zod schema as json schema (#2206) --- .gitignore | 5 ++++- package.json | 3 ++- scripts/generate-json-schema.ts | 35 +++++++++++++++++++++++++++++++++ src/types/comfyWorkflow.ts | 2 +- 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 scripts/generate-json-schema.ts diff --git a/.gitignore b/.gitignore index 5fc7f8c41..6f3c6f424 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,7 @@ browser_tests/*/*-win32.png dist.zip -/temp/ \ No newline at end of file +/temp/ + +# Generated JSON Schemas +/schemas/ diff --git a/package.json b/package.json index 9dceae9eb..38f98f962 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "lint": "eslint src", "lint:fix": "eslint src --fix", "locale": "lobe-i18n locale", - "collect-i18n": "playwright test --config=playwright.i18n.config.ts" + "collect-i18n": "playwright test --config=playwright.i18n.config.ts", + "json-schema": "tsx scripts/generate-json-schema.ts" }, "devDependencies": { "@babel/core": "^7.24.7", diff --git a/scripts/generate-json-schema.ts b/scripts/generate-json-schema.ts new file mode 100644 index 000000000..406ba538b --- /dev/null +++ b/scripts/generate-json-schema.ts @@ -0,0 +1,35 @@ +import fs from 'fs' +import path from 'path' +import { zodToJsonSchema } from 'zod-to-json-schema' + +import { zComfyWorkflow, zComfyWorkflow1 } from '../src/types/comfyWorkflow' + +// Convert both workflow schemas to JSON Schema +const workflow04Schema = zodToJsonSchema(zComfyWorkflow, { + name: 'ComfyWorkflow0_4', + $refStrategy: 'none' +}) + +const workflow1Schema = zodToJsonSchema(zComfyWorkflow1, { + name: 'ComfyWorkflow1_0', + $refStrategy: 'none' +}) + +// Create output directory if it doesn't exist +const outputDir = './schemas' +if (!fs.existsSync(outputDir)) { + fs.mkdirSync(outputDir, { recursive: true }) +} + +// Write schemas to files +fs.writeFileSync( + path.join(outputDir, 'workflow-0_4.json'), + JSON.stringify(workflow04Schema, null, 2) +) + +fs.writeFileSync( + path.join(outputDir, 'workflow-1_0.json'), + JSON.stringify(workflow1Schema, null, 2) +) + +console.log('JSON Schemas generated successfully!') diff --git a/src/types/comfyWorkflow.ts b/src/types/comfyWorkflow.ts index 4eade8dd8..7598438a1 100644 --- a/src/types/comfyWorkflow.ts +++ b/src/types/comfyWorkflow.ts @@ -200,7 +200,7 @@ export const zComfyWorkflow = z .passthrough() /** Schema version 1 */ -const zComfyWorkflow1 = z +export const zComfyWorkflow1 = z .object({ version: z.literal(1), config: zConfig.optional().nullable(),