{"record":{"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/2d90846af918af9602e088812d63a035d47cdbe4/packages/zod/src/v4/core/util.ts#L589-L625","documentation":"Thrown by the internal pick() helper (the engine behind `.pick()`). Zod v4 attaches refinements (`.refine()`, `.check()`) to the object schema itself via `def.checks`, and `.pick()` produces a new schema with a fresh empty checks array. Silently dropping refinements during a sub-selection would be a correctness hazard, so the operation is hard-blocked whenever `def.checks.length > 0`.","triggerScenarios":"Calling `myObject.pick({ a: true })` on an object that was built with `.refine(...)`, `.refine(...).refine(...)`, `.check(...)`, or any check that lands on the object node. The throw happens eagerly at the `.pick()` call, not at parse time.","commonSituations":"Building a base schema with a cross-field refinement (e.g. password/confirm equality) and then sub-selecting fields for a form subset; sharing a domain schema that carries invariants and slicing it for an API DTO; refactoring a flat schema by adding `.refine()` upstream of an existing `.pick()`.","solutions":["Move the refinement onto the relevant leaf field or re-apply it after `.pick()` so the new schema owns it explicitly.","Drop the refinement from the source object before picking (or use a refinement-free variant of the schema for the DTO).","Build the picked schema from scratch and add the refinement scoped to the reduced field set."],"exampleFix":"// before\nconst User = z.object({ a: z.string(), b: z.string() }).refine(v => v.a === v.b);\nconst Picked = User.pick({ a: true }); // throws\n\n// after\nconst Picked = z.object({ a: z.string() });\n// re-apply only the refinement logic that still applies","handlingStrategy":"validation","validationCode":"function canPick(schema: z.ZodObject): boolean {\n  const checks = (schema as any)._zod?.def?.checks;\n  return !checks || checks.length === 0;\n}\n// if (!canPick(MyObject)) { /* refactor: 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 refinement-free base schemas for slicing (pick/omit/partial) and layer refinements on per-variant.","Prefer field-level `.refine()` over object-level when you also need to transform the schema.","Document which schemas are 'safe to slice' vs 'carry invariants'."],"tags":["schema-transformation","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"}