{"record":{"id":"e5bee1fc45abd1f0","repo":"colinhacks/zod","slug":"omit-cannot-be-used-on-object-schemas-containin","errorCode":null,"errorMessage":".omit() cannot be used on object schemas containing refinements","messagePattern":"\\.omit\\(\\) cannot be used on object schemas containing refinements","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/zod/src/v4/core/util.ts","lineNumber":636,"sourceCode":"        newShape[key] = currDef.shape[key]!;\n      }\n\n      assignProp(this, \"shape\", newShape); // self-caching\n      return newShape;\n    },\n    checks: [],\n  });\n\n  return clone(schema, def) as any;\n}\n\nexport function omit(schema: schemas.$ZodObject, mask: object): any {\n  const currDef = schema._zod.def;\n\n  const checks = currDef.checks;\n  const hasChecks = checks && checks.length > 0;\n  if (hasChecks) {\n    throw new Error(\".omit() cannot be used on object schemas containing refinements\");\n  }\n\n  const def = mergeDefs(schema._zod.def, {\n    get shape() {\n      const newShape: Writeable<schemas.$ZodShape> = { ...schema._zod.def.shape };\n      for (const key in mask) {\n        if (!(key in currDef.shape)) {\n          throw new Error(`Unrecognized key: \"${key}\"`);\n        }\n        if (!(mask as any)[key]) continue;\n\n        delete newShape[key];\n      }\n      assignProp(this, \"shape\", newShape); // self-caching\n      return newShape;\n    },\n    checks: [],\n  });","sourceCodeStart":618,"sourceCodeEnd":654,"githubUrl":"https://github.com/colinhacks/zod/blob/2d90846af918af9602e088812d63a035d47cdbe4/packages/zod/src/v4/core/util.ts#L618-L654","documentation":"Thrown by the omit() helper. Identical contract to `.pick()`: `.omit()` rebuilds the schema with an empty checks array, so dropping object-level refinements silently is forbidden. The guard fires whenever `def.checks.length > 0` on the source object.","triggerScenarios":"Calling `myObject.omit({ secret: true })` where `myObject` has a `.refine()`, `.check()`, or any object-level refinement attached. Eager throw at the `.omit()` call site.","commonSituations":"Stripping sensitive fields (passwordHash, internal flags) from a schema that also enforces a cross-field invariant; reusing a domain entity schema for a public response shape; adding a refinement upstream and forgetting an existing `.omit()` depends on it.","solutions":["Re-apply the refinement to the omitted result so it is not silently lost.","Factor out a refinement-free base schema and call `.omit()` on that, layering refinements on each variant.","Build the public schema from scratch with only the needed fields."],"exampleFix":"// before\nconst User = z.object({ email: z.string(), pwd: z.string() })\n  .refine(v => v.email.length > 0);\nconst Public = User.omit({ pwd: true }); // throws\n\n// after\nconst Public = z.object({ email: z.string() })\n  .refine(v => v.email.length > 0);","handlingStrategy":"validation","validationCode":"function canOmit(schema: z.ZodObject): boolean {\n  const checks = (schema as any)._zod?.def?.checks;\n  return !checks || checks.length === 0;\n}\n// if (!canOmit(MyObject)) { /* drop refinement or rebuild */ }","typeGuard":"function hasObjectRefinements(schema: z.ZodObject): boolean {\n  const checks = (schema as any)._zod?.def?.checks;\n  return Array.isArray(checks) && checks.length > 0;\n}","tryCatchPattern":null,"preventionTips":["Keep a refinement-free base for DTO/subset construction.","Re-apply refinements explicitly after `.omit()` on a clean base.","Avoid attaching cross-field refinements to schemas you intend to slice."],"tags":["schema-transformation","omit","refinement","object-schema","zod-v4"],"backgroundTag":null,"analyzedSha":"2d90846af918af9602e088812d63a035d47cdbe4","analyzedAt":"2026-08-11T01:21:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-22T11:17:16.035Z"}