{"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/912f0f51b0ced654d0069741e7160834dca742ee/packages/zod/src/v4/core/util.ts#L618-L654","documentation":"Thrown by `.omit()` when the target object schema has refinement checks attached (`.refine()`, `.superRefine()`, or similar) — see the `hasChecks` guard at util.ts:633-635. Refinements operate over the whole object, so dropping fields via `.omit()` would silently invalidate them; the library refuses rather than guess. The message points the user toward restructuring instead.","triggerScenarios":"Calling `Schema.omit({...})` on a schema built as `z.object({...}).refine(...)` or `z.object({...}).superRefine(...)`, or composing with any check that lands in `def.checks`.","commonSituations":"Building a create/update variant of a validated schema by omitting fields (e.g. `User.omit({ id: true })`) when `User` was defined with cross-field validation; refactoring validation into the base object; migrating from v3 patterns that allowed this.","solutions":["Move the refinement onto a separate schema after `.omit()`: define the bare object, `omit` it, then attach `.refine()` to the result.","Replace the refinement with per-field checks (`.refine()` on individual fields) so it survives `.omit()`.","Use `.safeExtend()` or rebuild the shape manually instead of `.omit()` when you must preserve the refinement."],"exampleFix":"// before\nconst User = z.object({ id: z.string(), email: z.string() }).refine(d => d.id !== d.email);\nconst NewUser = User.omit({ id: true }); // throws\n\n// after\nconst Base = z.object({ id: z.string(), email: z.string() });\nconst NewUser = Base.omit({ id: true }).refine(d => d.id !== d.email);","handlingStrategy":"type-guard","validationCode":"function hasRefinements(s: z.core.$ZodObject): boolean {\n  return !!s._zod.def.checks?.length;\n}\nif (hasRefinements(User)) {\n  // omit on a bare variant instead\n}","typeGuard":"function isRefinementFree(s: z.core.$ZodObject): s is z.core.$ZodObject & { _zod: { def: { checks: [] } } } {\n  return !s._zod.def.checks?.length;\n}","tryCatchPattern":"try { const Out = User.omit({ id: true }); } catch (e) {\n  if (e instanceof Error && /omit.*refinements/.test(e.message)) {\n    const Out = Bare.omit({ id: true }).refine(/* original */);\n  } else throw e;\n}","preventionTips":["Define base objects without refinements and attach `.refine()` only on the final composed schema.","Prefer per-field refinements over object-level ones when you'll need `.omit()`."],"tags":["zod","object-schema","omit","refinement"],"analyzedSha":"912f0f51b0ced654d0069741e7160834dca742ee","analyzedAt":"2026-08-03T17:41:55.908Z","schemaVersion":2}