Skip to content

Commit 222a663

Browse files
committed
Change ZodObject default to $strip. Set inst.shape in zod mini
1 parent de3517e commit 222a663

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ export function keyof<T extends ZodObject>(schema: T): ZodLiteral<keyof T["_zod"
10251025
export interface ZodObject<
10261026
/** @ts-ignore Cast variance */
10271027
out Shape extends core.$ZodShape = core.$ZodLooseShape,
1028-
out Config extends core.$ZodObjectConfig = core.$ZodObjectConfig,
1028+
out Config extends core.$ZodObjectConfig = core.$strip,
10291029
> extends _ZodType<core.$ZodObjectInternals<Shape, Config>>,
10301030
core.$ZodObject<Shape, Config> {
10311031
shape: Shape;
@@ -1108,9 +1108,7 @@ export const ZodObject: core.$constructor<ZodObject> = /*@__PURE__*/ core.$const
11081108
core.$ZodObject.init(inst, def);
11091109
ZodType.init(inst, def);
11101110

1111-
util.defineLazy(inst, "shape", () => {
1112-
return Object.fromEntries(Object.entries(inst._zod.def.shape));
1113-
});
1111+
util.defineLazy(inst, "shape", () => def.shape);
11141112
inst.keyof = () => _enum(Object.keys(inst._zod.def.shape)) as any;
11151113
inst.catchall = (catchall) => inst.clone({ ...inst._zod.def, catchall: catchall as any as core.$ZodType }) as any;
11161114
inst.passthrough = () => inst.clone({ ...inst._zod.def, catchall: unknown() });

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,13 +433,35 @@ test("passthrough index signature", () => {
433433
// });
434434

435435
test("assignability", () => {
436-
z.object({ a: z.string() }) satisfies z.ZodObject;
437-
z.object({ a: z.string() }).catchall(z.number()) satisfies z.ZodObject;
436+
z.object({ a: z.string() }) satisfies z.ZodObject<{ a: z.ZodString }>;
437+
z.object({ a: z.string() }).catchall(z.number()) satisfies z.ZodObject<{ a: z.ZodString }>;
438438
z.object({ a: z.string() }).strict() satisfies z.ZodObject;
439439
z.object({}) satisfies z.ZodObject;
440-
z.object({ "a?": z.string() }) satisfies z.ZodObject;
441-
z.object({ "?a": z.string() }) satisfies z.ZodObject;
442440

441+
z.looseObject({ name: z.string() }) satisfies z.ZodObject<
442+
{
443+
name: z.ZodString;
444+
},
445+
z.core.$loose
446+
>;
447+
z.looseObject({ name: z.string() }) satisfies z.ZodObject<{
448+
name: z.ZodString;
449+
}>;
450+
z.strictObject({ name: z.string() }) satisfies z.ZodObject<
451+
{
452+
name: z.ZodString;
453+
},
454+
z.core.$loose
455+
>;
456+
z.strictObject({ name: z.string() }) satisfies z.ZodObject<
457+
{
458+
name: z.ZodString;
459+
},
460+
z.core.$strict
461+
>;
462+
z.object({ name: z.string() }) satisfies z.ZodObject<{
463+
name: z.ZodString;
464+
}>;
443465
z.object({
444466
a: z.string(),
445467
b: z.number(),

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ export function keyof<T extends ZodMiniObject>(schema: T): ZodMiniLiteral<keyof
695695
export interface ZodMiniObject<
696696
/** @ts-ignore Cast variance */
697697
out Shape extends core.$ZodShape = core.$ZodShape,
698-
out Config extends core.$ZodObjectConfig = core.$ZodObjectConfig,
698+
out Config extends core.$ZodObjectConfig = core.$strip,
699699
> extends ZodMiniType<any, any, core.$ZodObjectInternals<Shape, Config>>,
700700
core.$ZodObject<Shape, Config> {
701701
shape: Shape;
@@ -705,6 +705,7 @@ export const ZodMiniObject: core.$constructor<ZodMiniObject> = /*@__PURE__*/ cor
705705
(inst, def) => {
706706
core.$ZodObject.init(inst, def);
707707
ZodMiniType.init(inst, def);
708+
util.defineLazy(inst, "shape", () => def.shape);
708709
}
709710
);
710711
export function object<T extends core.$ZodLooseShape = Record<never, SomeType>>(

play.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
import { z } from "zod/v4";
22

3-
z;
4-
5-
console.dir(z.literal(Number.POSITIVE_INFINITY).parse(Number.POSITIVE_INFINITY), { depth: null });
3+
z.string().regex(/asdf/);

0 commit comments

Comments
 (0)