Skip to content
Open
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
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
],
"scripts": {
"build": "vite build && tsc",
"prepare": "npm run build",
"prepublishOnly": "npm run build",
"analyze": "vite build --mode analyze",
"test": "vitest",
Expand Down
1 change: 1 addition & 0 deletions packages/zod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
],
"scripts": {
"build": "vite build && tsc",
"prepare": "pnpm -C ../.. build:core && pnpm run build",
"prepublishOnly": "npm run build",
"analyze": "vite build --mode analyze"
},
Expand Down
13 changes: 13 additions & 0 deletions packages/zod/src/components/RenderTemplate.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react";
import type * as z from "zod/v4/core";
import { RenderTemplateProps } from "./types";
import { useTemplates } from "..";
import { unwrapSchema } from "../utils";
Expand All @@ -24,6 +25,7 @@ export const RenderTemplate: React.FC<RenderTemplateProps> = ({
UnionTemplate,
TupleTemplate,
EnumTemplate,
LiteralTemplate,
} = useTemplates();

// Let's first unwrap the schema to get to the core type
Expand Down Expand Up @@ -62,6 +64,17 @@ export const RenderTemplate: React.FC<RenderTemplateProps> = ({
case "tuple":
return <TupleTemplate schema={schema} path={path} />;

case "literal":
return <LiteralTemplate schema={schema} path={path} />;

case "lazy": {
// Lazy schemas contain a getter function that returns the actual schema
const lazySchema = coreSchema as z.$ZodLazy;
const actualSchema = lazySchema._zod.def.getter();
// Recursively render the actual schema
return <RenderTemplate schema={actualSchema} path={path} />;
}

default:
// Unknown or unsupported schema type
console.error(
Expand Down
1 change: 1 addition & 0 deletions packages/zod/src/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export type Templates = {
UnionTemplate: React.FC<{ schema: z.$ZodType; path: string[] }>;
TupleTemplate: React.FC<{ schema: z.$ZodType; path: string[] }>;
EnumTemplate: React.FC<{ schema: z.$ZodType; path: string[] }>;
LiteralTemplate: React.FC<{ schema: z.$ZodType; path: string[] }>;
};

/**
Expand Down
13 changes: 13 additions & 0 deletions packages/zod/src/utils/generateInitialData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ export function generateInitialData(schema: z.$ZodType): ZeroState {
return enumDef.entries[firstKey];
}

case "literal": {
const literalSchema = coreSchema as z.$ZodLiteral;
return Array.from(literalSchema._zod.def.values)[0];
}

case "lazy": {
// Lazy schemas contain a getter function that returns the actual schema
const lazySchema = coreSchema as z.$ZodLazy;
const actualSchema = lazySchema._zod.def.getter();
// Recursively generate initial data for the actual schema
return generateInitialData(actualSchema);
}

default:
console.error(`Unsupported schema type: ${def.type}`);
return undefined;
Expand Down
6 changes: 6 additions & 0 deletions packages/zod/src/utils/unwrapSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export function unwrapSchema(schema: z.$ZodType): z.$ZodTypes {
return unwrapSchema(nonOptionalSchema._zod.def.innerType);
}

case "lazy": {
const lazySchema = typedSchema as z.$ZodLazy;
const actualSchema = lazySchema._zod.def.getter();
return unwrapSchema(actualSchema);
}

default:
return typedSchema;
}
Expand Down