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
28 changes: 28 additions & 0 deletions packages/zod/src/v4/classic/tests/pickomit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,31 @@ test("pick/omit/required/partial - do not allow unknown keys", () => {
// @ts-expect-error
expect(() => schema.partial({ $unknown: true }).safeParse({})).toThrow();
});

test("omit keep refine functions", () => {
const schema = z
.object({
value: z.number(),
allowsNegative: z.boolean(),
somethingElse: z.string(),
})
.refine(({ value, allowsNegative }) => {
if (allowsNegative) {
return true;
}
return value >= 0;
});

const schemaOmit = schema.omit({
somethingElse: true,
});

const schemaBsafeparsedBad = schemaOmit.safeParse({ value: -5, allowsNegative: false, somethingElse: "" });
expect(schemaBsafeparsedBad.success).toEqual(false);

const schemaBsafeparsedOk2 = schemaOmit.safeParse({ value: 5, allowsNegative: false, somethingElse: "" });
expect(schemaBsafeparsedOk2.success).toEqual(true);

const schemaBsafeparsedOk = schemaOmit.safeParse({ value: -5, allowsNegative: true, somethingElse: "" });
expect(schemaBsafeparsedOk.success).toEqual(true);
});
2 changes: 1 addition & 1 deletion packages/zod/src/v4/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ export function omit(schema: schemas.$ZodObject, mask: object): any {
assignProp(this, "shape", newShape); // self-caching
return newShape;
},
checks: [],
checks: currDef.checks,
});

return clone(schema, def);
Expand Down