{"record":{"id":"cdf986ca284cbf55","repo":"can1357/oh-my-pi","slug":"cannot-apply-max-to-ir-k","errorCode":null,"errorMessage":"cannot apply max to ${ir.k}","messagePattern":"cannot apply max to (.+?)","errorType":"exception","errorClass":"OmpTypeError","httpStatus":null,"severity":"error","filePath":"packages/omptype/src/zod.ts","lineNumber":157,"sourceCode":"\t\t\t\tif (Number.isNaN(bound)) throw new OmpTypeError(\"number min must not be NaN\");\n\t\t\t\tif (ir.min !== undefined && ir.min >= bound) return next(restrictBase(schema, ir));\n\t\t\t\treturn next(restrictBase(schema, { ...ir, min: bound, xmin: false }));\n\t\t\t}\n\t\t\tthrow new OmpTypeError(`cannot apply min to ${ir.k}`);\n\t\t},\n\t\tmax(bound: number): ZodLikeSchema<Out> {\n\t\t\tconst ir = schema.ir;\n\t\t\tif (ir.k === \"string\" || ir.k === \"array\") {\n\t\t\t\tlengthBound(\"max\", schema, bound);\n\t\t\t\tconst max = ir.max === undefined ? bound : Math.min(ir.max, bound);\n\t\t\t\treturn next(restrictBase(schema, { ...ir, max }));\n\t\t\t}\n\t\t\tif (ir.k === \"number\") {\n\t\t\t\tif (Number.isNaN(bound)) throw new OmpTypeError(\"number max must not be NaN\");\n\t\t\t\tif (ir.max !== undefined && ir.max <= bound) return next(restrictBase(schema, ir));\n\t\t\t\treturn next(restrictBase(schema, { ...ir, max: bound, xmax: false }));\n\t\t\t}\n\t\t\tthrow new OmpTypeError(`cannot apply max to ${ir.k}`);\n\t\t},\n\t\tint(): ZodLikeSchema<Out> {\n\t\t\tif (schema.ir.k !== \"number\") throw new OmpTypeError(`cannot apply int to ${schema.ir.k}`);\n\t\t\treturn next(restrictBase(schema, { ...schema.ir, int: true }));\n\t\t},\n\t\tpositive(): ZodLikeSchema<Out> {\n\t\t\tif (schema.ir.k !== \"number\") throw new OmpTypeError(`cannot apply positive to ${schema.ir.k}`);\n\t\t\tconst ir = schema.ir;\n\t\t\tif (ir.min !== undefined && ir.min > 0) return next(restrictBase(schema, ir));\n\t\t\treturn next(restrictBase(schema, { ...ir, min: 0, xmin: true }));\n\t\t},\n\t\tnonnegative(): ZodLikeSchema<Out> {\n\t\t\tif (schema.ir.k !== \"number\") throw new OmpTypeError(`cannot apply nonnegative to ${schema.ir.k}`);\n\t\t\treturn this.min(0);\n\t\t},\n\t\tregex(expression: RegExp, message?: string): ZodLikeSchema<Out> {\n\t\t\tif (schema.ir.k !== \"string\") throw new OmpTypeError(`cannot apply regex to ${schema.ir.k}`);\n\t\t\tconst expectation = message ?? `matching ${expression}`;","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/omptype/src/zod.ts#L139-L175","documentation":"`.max()` only applies to string and number schemas; calling it on any other kind (object, boolean, date, array-of-union, etc.) throws this OmpTypeError. The message interpolates the actual IR kind (`ir.k`) of the schema it was called on. Thrown from `max()` at packages/omptype/src/zod.ts:157.","triggerScenarios":"`t.boolean().max(1)`, `t.object({...}).max(5)`, or any schema whose underlying IR kind is not \"string\", \"array\", or \"number\" having `.max()` called on it.","commonSituations":"Copy-pasted validation chains where the schema kind was changed but the `.max()` call was left behind; generic helper functions that apply bounds without knowing the schema kind; refactors where a field switched from string to boolean.","solutions":["Ensure the schema is a string or number schema before calling .max()","Move the .max() call to the correct field's schema in the shape","If writing generic code, branch on schema kind (or use a type-level constraint) before applying bounds"],"exampleFix":"// before\nt.object({ enabled: t.boolean() }).max(10);\n// after\nt.object({ enabled: t.boolean() });\n// or apply max to the numeric field:\nt.object({ count: t.number().max(10) });","handlingStrategy":"type-guard","validationCode":"function canApplyMax(schema: ZodLikeSchema<unknown>): boolean {\n  const k = schema.ir.k;\n  return k === \"string\" || k === \"number\" || k === \"array\";\n}\nif (!canApplyMax(schema)) throw new Error(\"schema kind does not support max\");","typeGuard":"function isBoundedKind(ir: { k: string }): ir is { k: \"string\" | \"number\" | \"array\" } {\n  return ir.k === \"string\" || ir.k === \"number\" || ir.k === \"array\";\n}","tryCatchPattern":"try {\n  schema = base.max(n);\n} catch (err) {\n  if (err instanceof OmpTypeError && err.message.startsWith(\"cannot apply max\")) {\n    throw new Error(`bug: .max() applied to schema of kind ${base.ir.k}`);\n  } else throw err;\n}","preventionTips":["Chain .max() immediately after t.string()/t.number() so the type system ties it to the right kind","In generic validators, branch on schema.ir.k before applying bounds","After refactoring a field's type, grep the chain for leftover constraint calls"],"tags":["schema-builder","type-mismatch","api-misuse"],"backgroundTag":"schema-method-kind-mismatch","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}