{"record":{"id":"e884583a4ea33b1f","repo":"can1357/oh-my-pi","slug":"cannot-apply-regex-to-schema-ir-k","errorCode":null,"errorMessage":"cannot apply regex to ${schema.ir.k}","messagePattern":"cannot apply regex to (.+?)","errorType":"exception","errorClass":"OmpTypeError","httpStatus":null,"severity":"error","filePath":"packages/omptype/src/zod.ts","lineNumber":174,"sourceCode":"\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 {\n\t\t\tconst widened = schema.or(type.raw(\"undefined\")) as Decoratable<Out | undefined>;\n\t\t\treturn decorate(widened, true) as ZodLikeSchema<Out | undefined> & OptionalSchemaMarker;\n\t\t},\n\t\tnullable(): ZodLikeSchema<Out | null> {","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/omptype/src/zod.ts#L156-L192","documentation":"`.regex()` tests a string value against a RegExp and is only valid on string schemas; calling it on any other kind throws this OmpTypeError. The message interpolates the actual IR kind. Thrown from `regex()` at packages/omptype/src/zod.ts:174.","triggerScenarios":"`t.number().regex(/.../)`, `.regex()` on boolean/object/array schemas; or forgetting that the value must already be a string at validation time.","commonSituations":"Validating patterns on values that come from inputs/APIs as strings but were typed as number in the schema; generic field validators that apply a pattern to every field; copy-paste between string and non-string field definitions.","solutions":["Call .regex() only on t.string() schemas","For numbers use numeric bounds/constraints instead of regex","If the raw value is a string containing a number, keep the field as t.string().regex(...) or transform to number first"],"exampleFix":"// before\nt.number().regex(/^\\d+$/)\n// after\nt.string().regex(/^\\d+$/)\n// or numeric constraint:\nt.number().int().nonnegative()","handlingStrategy":"type-guard","validationCode":"function canApplyRegex(schema: ZodLikeSchema<unknown>): boolean {\n  return schema.ir.k === \"string\";\n}\nif (!canApplyRegex(schema)) throw new Error(\"regex() requires a string schema\");","typeGuard":"function isStringSchema(ir: { k: string }): ir is { k: \"string\" } {\n  return ir.k === \"string\";\n}","tryCatchPattern":"try {\n  schema = base.regex(pattern);\n} catch (err) {\n  if (err instanceof OmpTypeError && err.message.startsWith(\"cannot apply regex\")) {\n    throw new Error(\"field must be t.string() to use .regex()\");\n  } else throw err;\n}","preventionTips":["Apply pattern checks only to string fields; validate numbers with bounds/int instead","In shared field-validation tables, store the schema kind next to the pattern and check compatibility","If values arrive as strings containing numbers, keep the field as t.string() for regex validation"],"tags":["schema-builder","type-mismatch","regex"],"backgroundTag":"schema-method-kind-mismatch","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}