Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 359c0db

Browse files
committed
Add .with() alias
1 parent 15cc73f commit 359c0db

5 files changed

Lines changed: 89 additions & 0 deletions

File tree

packages/zod/src/v4/classic/schemas.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface ZodType<
3838

3939
// base methods
4040
check(...checks: (core.CheckFn<core.output<this>> | core.$ZodCheck<core.output<this>>)[]): this;
41+
with(...checks: (core.CheckFn<core.output<this>> | core.$ZodCheck<core.output<this>>)[]): this;
4142
clone(def?: Internals["def"], params?: { parent: boolean }): this;
4243
register<R extends core.$ZodRegistry>(
4344
registry: R,
@@ -180,6 +181,7 @@ export const ZodType: core.$constructor<ZodType> = /*@__PURE__*/ core.$construct
180181
}
181182
);
182183
};
184+
inst.with = inst.check;
183185
inst.clone = (def, params) => core.clone(inst, def, params);
184186
inst.brand = () => inst as any;
185187
inst.register = ((reg: any, meta: any) => {

packages/zod/src/v4/classic/tests/index.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,47 @@ test("z.check", () => {
676676
});
677677
});
678678

679+
test("z.with (alias for z.check)", () => {
680+
// .with() should work exactly the same as .check()
681+
const a = z.any().with(
682+
z.check<string>((ctx) => {
683+
if (typeof ctx.value === "string") return;
684+
ctx.issues.push({
685+
code: "custom",
686+
origin: "custom",
687+
message: "Expected a string",
688+
input: ctx.value,
689+
});
690+
})
691+
);
692+
expect(z.safeParse(a, "hello")).toMatchObject({
693+
success: true,
694+
data: "hello",
695+
});
696+
expect(z.safeParse(a, 123)).toMatchObject({
697+
success: false,
698+
error: { issues: [{ code: "custom", message: "Expected a string" }] },
699+
});
700+
701+
// Test with refine
702+
const b = z.string().with(z.refine((val) => val.length > 3, "Must be longer than 3"));
703+
expect(z.safeParse(b, "hello").success).toBe(true);
704+
expect(z.safeParse(b, "hi").success).toBe(false);
705+
706+
// Test with function
707+
const c = z.string().with(({ value, issues }) => {
708+
if (value.length <= 3) {
709+
issues.push({
710+
code: "custom",
711+
input: value,
712+
message: "Must be longer than 3",
713+
});
714+
}
715+
});
716+
expect(z.safeParse(c, "hello").success).toBe(true);
717+
expect(z.safeParse(c, "hi").success).toBe(false);
718+
});
719+
679720
test("z.instanceof", () => {
680721
class A {}
681722

packages/zod/src/v4/mini/schemas.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface ZodMiniType<
1111
> extends core.$ZodType<Output, Input, Internals> {
1212
type: Internals["def"]["type"];
1313
check(...checks: (core.CheckFn<core.output<this>> | core.$ZodCheck<core.output<this>>)[]): this;
14+
with(...checks: (core.CheckFn<core.output<this>> | core.$ZodCheck<core.output<this>>)[]): this;
1415
refine<Ch extends (arg: core.output<this>) => unknown | Promise<unknown>>(
1516
check: Ch,
1617
params?: string | core.$ZodCustomParams
@@ -69,6 +70,7 @@ export const ZodMiniType: core.$constructor<ZodMiniType> = /*@__PURE__*/ core.$c
6970
{ parent: true }
7071
);
7172
};
73+
inst.with = inst.check;
7274
inst.refine = (check, params) => inst.check(refine(check, params)) as never;
7375
inst.clone = (_def, params) => core.clone(inst, _def, params);
7476
inst.brand = () => inst as any;

packages/zod/src/v4/mini/tests/index.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,47 @@ test("z.check", () => {
672672
});
673673
});
674674

675+
test("z.with (alias for z.check)", () => {
676+
// .with() should work exactly the same as .check()
677+
const a = z.any().with(
678+
z.check<string>((ctx) => {
679+
if (typeof ctx.value === "string") return;
680+
ctx.issues.push({
681+
code: "custom",
682+
origin: "custom",
683+
message: "Expected a string",
684+
input: ctx.value,
685+
});
686+
})
687+
);
688+
expect(z.safeParse(a, "hello")).toMatchObject({
689+
success: true,
690+
data: "hello",
691+
});
692+
expect(z.safeParse(a, 123)).toMatchObject({
693+
success: false,
694+
error: { issues: [{ code: "custom", message: "Expected a string" }] },
695+
});
696+
697+
// Test with refine
698+
const b = z.string().with(z.refine((val) => val.length > 3, "Must be longer than 3"));
699+
expect(z.safeParse(b, "hello").success).toBe(true);
700+
expect(z.safeParse(b, "hi").success).toBe(false);
701+
702+
// Test with function
703+
const c = z.string().with(({ value, issues }) => {
704+
if (value.length <= 3) {
705+
issues.push({
706+
code: "custom",
707+
input: value,
708+
message: "Must be longer than 3",
709+
});
710+
}
711+
});
712+
expect(z.safeParse(c, "hello").success).toBe(true);
713+
expect(z.safeParse(c, "hi").success).toBe(false);
714+
});
715+
675716
test("z.instanceof", () => {
676717
class A {}
677718

play.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
import * as z from "zod";
22

33
z;
4+
5+
z.string().slugify().parse("Hello World");
6+
// => "hello-world"

0 commit comments

Comments
 (0)