Skip to content

Commit 0e803a2

Browse files
committed
Revert "Do not allow unsound pick/omit"
This reverts commit 0f4ce73.
1 parent 5bfc8f2 commit 0e803a2

File tree

2 files changed

+5
-32
lines changed

2 files changed

+5
-32
lines changed

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -607,25 +607,3 @@ test("safeExtend() on object with refinements should not throw", () => {
607607

608608
expect(() => schema.safeExtend({ b: z.string() })).not.toThrow();
609609
});
610-
611-
test("pick() on object with refinements should throw", () => {
612-
const schema = z
613-
.object({
614-
a: z.string(),
615-
b: z.number(),
616-
})
617-
.refine(() => true);
618-
619-
expect(() => schema.pick({ a: true })).toThrow("Invalid .pick() on object schemas containing refinements");
620-
});
621-
622-
test("omit() on object with refinements should throw", () => {
623-
const schema = z
624-
.object({
625-
a: z.string(),
626-
b: z.number(),
627-
})
628-
.refine(() => true);
629-
630-
expect(() => schema.omit({ b: true })).toThrow("Invalid .omit() on object schemas containing refinements");
631-
});

packages/zod/src/v4/core/util.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -592,10 +592,8 @@ export const BIGINT_FORMAT_RANGES: Record<checks.$ZodBigIntFormats, [bigint, big
592592

593593
export function pick(schema: schemas.$ZodObject, mask: Record<string, unknown>): any {
594594
const currDef = schema._zod.def;
595-
if ((currDef.checks ?? []).length > 0) {
596-
throw new Error("Invalid .pick() on object schemas containing refinements");
597-
}
598-
const def = mergeDefs(currDef, {
595+
596+
const def = mergeDefs(schema._zod.def, {
599597
get shape() {
600598
const newShape: Writeable<schemas.$ZodShape> = {};
601599
for (const key in mask) {
@@ -617,13 +615,10 @@ export function pick(schema: schemas.$ZodObject, mask: Record<string, unknown>):
617615

618616
export function omit(schema: schemas.$ZodObject, mask: object): any {
619617
const currDef = schema._zod.def;
620-
if ((currDef.checks ?? []).length > 0) {
621-
throw new Error("Invalid .omit() on object schemas containing refinements");
622-
}
623618

624-
const def = mergeDefs(currDef, {
619+
const def = mergeDefs(schema._zod.def, {
625620
get shape() {
626-
const newShape: Writeable<schemas.$ZodShape> = { ...currDef.shape };
621+
const newShape: Writeable<schemas.$ZodShape> = { ...schema._zod.def.shape };
627622
for (const key in mask) {
628623
if (!(key in currDef.shape)) {
629624
throw new Error(`Unrecognized key: "${key}"`);
@@ -649,7 +644,7 @@ export function extend(schema: schemas.$ZodObject, shape: schemas.$ZodShape): an
649644
const checks = schema._zod.def.checks;
650645
const hasChecks = checks && checks.length > 0;
651646
if (hasChecks) {
652-
throw new Error("Invalid .extend() on object schemas containing refinements. Use .safeExtend() instead.");
647+
throw new Error("Object schemas containing refinements cannot be extended. Use `.safeExtend()` instead.");
653648
}
654649

655650
const def = mergeDefs(schema._zod.def, {

0 commit comments

Comments
 (0)