{"record":{"id":"063b2c54ad0de70e","repo":"can1357/oh-my-pi","slug":"object-mode-requires-an-object-schema","errorCode":null,"errorMessage":"object mode requires an object schema","messagePattern":"object mode requires an object schema","errorType":"exception","errorClass":"OmpTypeError","httpStatus":null,"severity":"error","filePath":"packages/omptype/src/zod.ts","lineNumber":109,"sourceCode":"function isStringKeyIR(ir: IR): boolean {\n\tswitch (ir.k) {\n\t\tcase \"string\":\n\t\t\treturn true;\n\t\tcase \"lit\":\n\t\t\treturn typeof ir.v === \"string\";\n\t\tcase \"union\":\n\t\t\treturn ir.members.length > 0 && ir.members.every(isStringKeyIR);\n\t\tcase \"sub\":\n\t\t\treturn isStringKeyIR(ir.schema.ir);\n\t\tdefault:\n\t\t\treturn false;\n\t}\n}\n\nfunction decorate<Out>(schema: Decoratable<Out>, optional = false): ZodLikeSchema<Out> {\n\tconst next = (inner: Decoratable<Out>, nextOptional = optional): ZodLikeSchema<Out> => decorate(inner, nextOptional);\n\tconst withObjectExtras = (extras: \"keep\" | \"reject\" | \"delete\"): ZodLikeSchema<Out> => {\n\t\tif (schema.ir.k !== \"object\") throw new OmpTypeError(\"object mode requires an object schema\");\n\t\treturn next(restrictBase(schema, { ...schema.ir, extras }));\n\t};\n\tObject.defineProperty(schema, \"isOptional\", { value: optional, enumerable: false });\n\n\treturn Object.assign(schema, {\n\t\tparse(value: unknown): Out {\n\t\t\tconst result = schema(value);\n\t\t\tif (result instanceof type.errors) throw new Error(result.summary);\n\t\t\treturn result;\n\t\t},\n\t\tsafeParse(value: unknown): ZodLikeSafeParseResult<Out> {\n\t\t\tconst result = schema(value);\n\t\t\tif (!(result instanceof type.errors)) return { success: true, data: result };\n\t\t\treturn {\n\t\t\t\tsuccess: false,\n\t\t\t\terror: {\n\t\t\t\t\tmessage: result.summary,\n\t\t\t\t\tissues: result.map(issue => ({ path: [...issue.path], message: issue.problem })),","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/omptype/src/zod.ts#L91-L127","documentation":"The zod-compat methods `.strict()`, `.passthrough()`, and `.strip()` only make sense for object schemas; they call `withObjectExtras`, which throws `object mode requires an object schema` when the underlying schema's IR kind is not `object`.","triggerScenarios":"`z.string().strict()`, `z.array(el).passthrough()`, `z.object({...}).strict()` is fine but `z.union([...]).strict()` throws; also calling strict/passthrough/strip on a schema that was narrowed from object to something else.","commonSituations":"Generic wrapper functions that unconditionally chain `.strict()` on schemas of varying kinds; copying a builder chain to a non-object type during refactors.","solutions":["Only call `.strict()`/`.passthrough()`/`.strip()` on `z.object({...})` schemas.","If the schema kind varies, gate the call: `if (schema.ir.k === 'object') schema.strict()` or use a typed TObject parameter.","Remove the mode call if the intent was only decoration — these methods are object-specific."],"exampleFix":"// before\nfunction define(s: any) { return s.strict(); }\ndefine(z.string()); // throws\n// after\nfunction define(s: ZodLikeSchema<{ id: string }>) { return s.strict(); }\ndefine(z.object({ id: z.string() }));","handlingStrategy":"type-guard","validationCode":"// narrow to object schemas before applying modes\nif (schema.ir.k !== \"object\") throw new Error(\"strict()/passthrough() require an object schema\");\nschema.strict();","typeGuard":"function isObjectSchema(s: ZodLikeSchema<unknown>): boolean {\n  return (s as unknown as { ir: { k: string } }).ir.k === \"object\";\n}","tryCatchPattern":"try {\n  const strict = maybeSchema.strict();\n} catch (err) {\n  if (err instanceof Error && err.message === 'object mode requires an object schema') {\n    return maybeSchema; // modes only apply to objects — pass through unchanged\n  }\n  throw err;\n}","preventionTips":["Restrict strict/passthrough/strip call sites to values typed as object schemas.","In generic helpers, check `schema.ir.k === 'object'` before applying a mode.","Avoid unconditionally chaining .strict() on schemas of dynamic kinds."],"tags":["omptype","zod-compat","object-schema","schema-construction"],"backgroundTag":"schema-type-mismatch","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}