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
29 changes: 29 additions & 0 deletions packages/zod/src/v4/classic/tests/to-json-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2628,6 +2628,35 @@ test("defaults/prefaults", () => {
expect(z.toJSONSchema(c, { io: "input" })).toMatchInlineSnapshot(`
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"default": 1234,
"type": "string",
}
`);
});

test("default preserved through pipe/transform (issue #6049)", () => {
// z.string().transform(s => s).default("hello") should emit default: "hello" in io: "input" mode
const schema = z
.string()
.transform((s) => s)
.default("hello");
expect(z.toJSONSchema(schema, { io: "input" })).toMatchInlineSnapshot(`
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"default": "hello",
"type": "string",
}
`);
// With a proper output terminus, default appears in output mode too
const schemaWithPipe = z
.string()
.transform((s) => s.toUpperCase())
.pipe(z.string())
.default("hello");
expect(z.toJSONSchema(schemaWithPipe, { io: "input" })).toMatchInlineSnapshot(`
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"default": "hello",
"type": "string",
}
`);
Expand Down
5 changes: 4 additions & 1 deletion packages/zod/src/v4/core/to-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ export function process<T extends schemas.$ZodType>(
if (ctx.io === "input" && isTransforming(schema)) {
// examples/defaults only apply to output type of pipe
delete result.schema.examples;
delete result.schema.default;
// ZodDefault explicitly declares the input-side default — preserve it even when the inner type transforms
if (def.type !== "default") {
delete result.schema.default;
}
}

// set prefault as default
Expand Down
Loading