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
6 changes: 5 additions & 1 deletion packages/zod/src/v4/classic/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ export interface ZodType<
pipe<T extends core.$ZodType<any, core.output<this>>>(
target: T | core.$ZodType<any, core.output<this>>
): ZodPipe<this, T>;
pipe<const T extends core.SomeType>(
target: T,
...rest: core.output<this> extends core.input<T> ? [] : ["Incompatible pipe target"]
): ZodPipe<this, T>;
readonly(): ZodReadonly<this>;

/** Returns a new instance that has been registered in `z.globalRegistry` with the specified description */
Expand Down Expand Up @@ -326,7 +330,7 @@ export const ZodType: core.$constructor<ZodType> = /*@__PURE__*/ core.$construct
return _catch(this, params);
},
pipe(target) {
return pipe(this, target);
return pipe(this, target as core.$ZodType);
},
readonly() {
return readonly(this);
Expand Down
23 changes: 23 additions & 0 deletions packages/zod/src/v4/classic/tests/pipe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@ test("string to number pipe async", async () => {
expect(await schema.parseAsync("1234")).toEqual(1234);
});

test("pipe preserves contextual typing and compatibility checks", () => {
z.string().pipe(z.transform((val) => val.toUpperCase()));

// @ts-expect-error incompatible pipe targets are still rejected
z.string().pipe(z.number());
});

test("pipe accepts branded output into unbranded input", () => {
const zodBrand = z.string().brand<"myBrand">();
const inputSchema = z.object({
a: z.number(),
c: zodBrand,
});
const validateSchema = z.object({
a: z.number(),
c: zodBrand,
});

inputSchema.transform((input) => input).pipe(validateSchema);
inputSchema.pipe(validateSchema);
inputSchema.pipe(inputSchema);
});

test("string with default fallback", () => {
const stringWithDefault = z
.pipe(
Expand Down
Loading