{"record":{"id":"cd127f6ceda6e9ab","repo":"can1357/oh-my-pi","slug":"cannot-apply-nonnegative-to-schema-ir-k","errorCode":null,"errorMessage":"cannot apply nonnegative to ${schema.ir.k}","messagePattern":"cannot apply nonnegative to (.+?)","errorType":"exception","errorClass":"OmpTypeError","httpStatus":null,"severity":"error","filePath":"packages/omptype/src/zod.ts","lineNumber":170,"sourceCode":"\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}`;\n\t\t\tconst narrowed = schema.narrow((value, ctx) => {\n\t\t\t\texpression.lastIndex = 0;\n\t\t\t\tconst matches = expression.test(value as string);\n\t\t\t\texpression.lastIndex = 0;\n\t\t\t\treturn matches || ctx.mustBe(expectation);\n\t\t\t});\n\t\t\treturn next(narrowed);\n\t\t},\n\t\turl(): ZodLikeSchema<Out> {\n\t\t\tif (schema.ir.k !== \"string\") throw new OmpTypeError(`cannot apply url to ${schema.ir.k}`);\n\t\t\treturn next(restrictBase(schema, { ...schema.ir, url: true }));\n\t\t},\n\t\toptional(): ZodLikeSchema<Out | undefined> & OptionalSchemaMarker {","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/omptype/src/zod.ts#L152-L188","documentation":"`.nonnegative()` sets min 0 (inclusive) on number schemas; on any other schema kind it throws this OmpTypeError before delegating to `.min(0)`. The message interpolates the actual IR kind. Thrown from `nonnegative()` at packages/omptype/src/zod.ts:170.","triggerScenarios":"`t.string().nonnegative()`, `.nonnegative()` on boolean/object/array/union schemas.","commonSituations":"Retyped fields (count/amount fields moving between string and number) that keep the sign constraint; generic validation helpers that apply .nonnegative() unconditionally; confusing array `.min(0)` length bounds with numeric nonnegativity.","solutions":["Call .nonnegative() only on t.number() schemas","For arrays use .min(0) directly (array min is a length bound), not .nonnegative()","Convert numeric-string fields to t.number() before applying sign constraints"],"exampleFix":"// before\nt.string().nonnegative()\n// after\nt.number().nonnegative()","handlingStrategy":"type-guard","validationCode":"function canApplyNonnegative(schema: ZodLikeSchema<unknown>): boolean {\n  return schema.ir.k === \"number\";\n}\nif (!canApplyNonnegative(schema)) throw new Error(\"nonnegative() requires a number schema\");","typeGuard":"function isNumberSchema(ir: { k: string }): ir is { k: \"number\" } {\n  return ir.k === \"number\";\n}","tryCatchPattern":"try {\n  schema = base.nonnegative();\n} catch (err) {\n  if (err instanceof OmpTypeError && err.message.startsWith(\"cannot apply nonnegative\")) {\n    throw new Error(\"field must be t.number() to use .nonnegative()\");\n  } else throw err;\n}","preventionTips":["For array length use .min(0) directly; reserve .nonnegative() for numbers","Chain .nonnegative() immediately after t.number()","Watch for retyped count/amount fields losing their number kind"],"tags":["schema-builder","type-mismatch","number"],"backgroundTag":"schema-method-kind-mismatch","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}