From 7c519a9cef843aff8a6de3b94ffc10b5b84e237d Mon Sep 17 00:00:00 2001 From: Jordan Ritter Date: Fri, 15 May 2026 13:00:36 -0700 Subject: [PATCH] fix: use Zod 4 native toJSONSchema() for schema conversion zod-to-json-schema returns empty schemas with Zod 4.x, causing standardSchemaToFormSchema and inferFormSchemaFromConfig tests to fail. Zod 4 exposes toJSONSchema() on schema objects; prefer that when available, falling back to zod-to-json-schema for Zod 3. --- .../hook-preview/form/schema/standard-schema.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/webview/hook-preview/form/schema/standard-schema.ts b/src/webview/hook-preview/form/schema/standard-schema.ts index 3f3f320af..9d6072c75 100644 --- a/src/webview/hook-preview/form/schema/standard-schema.ts +++ b/src/webview/hook-preview/form/schema/standard-schema.ts @@ -4,6 +4,8 @@ import type { FormSchema } from "./types"; interface StandardSchemaLike { "~standard"?: { vendor?: string; version?: number }; + /** Zod 4+ exposes a native toJSONSchema() method on schema objects. */ + toJSONSchema?: () => Record; } export function standardSchemaToFormSchema(schema: unknown): FormSchema { @@ -12,8 +14,14 @@ export function standardSchemaToFormSchema(schema: unknown): FormSchema { if (vendor === "zod") { try { - const json = zodToJsonSchema( - schema as Parameters[0], + // Zod 4+ has a native toJSONSchema(); prefer it over zod-to-json-schema + // which doesn't fully support Zod 4's internal schema representation. + const json = ( + typeof s?.toJSONSchema === "function" + ? s.toJSONSchema() + : zodToJsonSchema( + schema as Parameters[0], + ) ) as JSONSchemaNode; return jsonSchemaToFormSchema(json); } catch (err) {