{"id":"a614f04c6326c4b0","repo":"colinhacks/zod","slug":"pick-cannot-be-used-on-object-schemas-containin","errorCode":null,"errorMessage":".pick() cannot be used on object schemas containing refinements","messagePattern":"\\.pick\\(\\) 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":607,"sourceCode":"  safeint: [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER],\n  int32: [-2147483648, 2147483647],\n  uint32: [0, 4294967295],\n  float32: [-3.4028234663852886e38, 3.4028234663852886e38],\n  float64: [-Number.MAX_VALUE, Number.MAX_VALUE],\n};\n\nexport const BIGINT_FORMAT_RANGES: Record<checks.$ZodBigIntFormats, [bigint, bigint]> = {\n  int64: [/* @__PURE__*/ BigInt(\"-9223372036854775808\"), /* @__PURE__*/ BigInt(\"9223372036854775807\")],\n  uint64: [/* @__PURE__*/ BigInt(0), /* @__PURE__*/ BigInt(\"18446744073709551615\")],\n};\n\nexport function pick(schema: schemas.$ZodObject, mask: Record<string, unknown>): 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(\".pick() 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> = {};\n      for (const key in mask) {\n        if (!(key in currDef.shape)) {\n          throw new Error(`Unrecognized key: \"${key}\"`);\n        }\n        if (!mask[key]) continue;\n        newShape[key] = currDef.shape[key]!;\n      }\n\n      assignProp(this, \"shape\", newShape); // self-caching\n      return newShape;\n    },\n    checks: [],\n  });","sourceCodeStart":589,"sourceCodeEnd":625,"githubUrl":"https://github.com/colinhacks/zod/blob/912f0f51b0ced654d0069741e7160834dca742ee/packages/zod/src/v4/core/util.ts#L589-L625","documentation":"Thrown by `util.pick` (the implementation behind `z.object(...).pick({...})`) when the object schema has refinements attached via `.refine()`, `.superRefine()`, or any check in `def.checks`. Picking a subset of keys cannot safely carry over whole-object refinements (which may reference the dropped keys), so Zod refuses rather than silently producing an unsound schema. The check happens at definition time, before the picked shape is built.","triggerScenarios":"Calling `.pick({ a: true })` (or `.omit(...)`) on an object that was refined, e.g. `z.object({ a, b }).refine((v) => v.a === v.b).pick({ a: true })`.","commonSituations":"Building a partial/preview type from a validated domain object that has cross-field invariants; reusing a base schema with `.refine` for an update/patch shape.","solutions":["Move the refinement OFF the base schema and onto a derived schema after `.pick()`/`.omit()`, so the base is refinement-free and pickable.","If you need both, define the raw shape, pick/omit it, then attach `.refine(...)` to the picked result.","Alternatively re-implement the relevant subset of the refinement as a new `.refine()` on the picked schema that only references the remaining keys."],"exampleFix":"// before\nconst Base = z.object({ a: z.string(), b: z.string() })\n  .refine((v) => v.a === v.b);\nconst Picked = Base.pick({ a: true }); // throws\n\n// after — refine AFTER picking\nconst Shape = z.object({ a: z.string(), b: z.string() });\nconst Picked = Shape.pick({ a: true });\nconst Base = Shape.refine((v) => v.a === v.b);","handlingStrategy":"validation","validationCode":"function isRefinementFree(schema) {\n  const checks = schema._zod?.def?.checks;\n  return !checks || checks.length === 0;\n}\nif (!isRefinementFree(objSchema)) throw new Error('cannot .pick() a refined object; refine after picking');","typeGuard":"function isRefinementFree(schema): boolean {\n  const checks = schema?._zod?.def?.checks;\n  return !checks || checks.length === 0;\n}","tryCatchPattern":"try {\n  objSchema.pick({ a: true });\n} catch (e) {\n  if (e instanceof Error && e.message === '.pick() cannot be used on object schemas containing refinements') {\n    // define the raw shape, pick from it, then re-attach the refinement to the result\n  }\n  throw e;\n}","preventionTips":["Keep base object schemas refinement-free; attach .refine()/.superRefine() to derived schemas after .pick()/.omit().","Factor cross-field invariants into a separate validator function applied where needed.","Audit .pick()/.omit() call sites against the base schema's checks array."],"tags":["object","pick","refinement","schema-definition"],"analyzedSha":"912f0f51b0ced654d0069741e7160834dca742ee","analyzedAt":"2026-08-03T17:41:55.908Z","schemaVersion":2}