{"record":{"id":"8a9a7ca8c81cdfc8","repo":"can1357/oh-my-pi","slug":"cannot-apply-url-to-schema-ir-k","errorCode":null,"errorMessage":"cannot apply url to ${schema.ir.k}","messagePattern":"cannot apply url to (.+?)","errorType":"exception","errorClass":"OmpTypeError","httpStatus":null,"severity":"error","filePath":"packages/omptype/src/zod.ts","lineNumber":185,"sourceCode":"\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> {\n\t\t\treturn decorate(schema.or(type.raw(\"null\")) as Decoratable<Out | null>, optional);\n\t\t},\n\t\tdefault(\n\t\t\tvalue: Exclude<Out, undefined> | (() => Exclude<Out, undefined>),\n\t\t): ZodLikeSchema<Exclude<Out, undefined>> {\n\t\t\ttype DefaultOut = Exclude<Out, undefined>;\n\t\t\tconst widened = schema.or(type.raw(\"undefined\")) as Decoratable<Out | undefined>;\n\t\t\tconst piped = widened.pipe(output => {\n\t\t\t\tif (output !== undefined) return output as DefaultOut;\n\t\t\t\treturn typeof value === \"function\" ? (value as () => DefaultOut)() : value;\n\t\t\t}) as Decoratable<DefaultOut>;","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/omptype/src/zod.ts#L167-L203","documentation":"`.url()` marks a string schema as requiring a valid URL; calling it on any other schema kind throws this OmpTypeError. The message interpolates the actual IR kind. Thrown from `url()` at packages/omptype/src/zod.ts:185.","triggerScenarios":"`t.number().url()`, `.url()` on boolean/object/union schemas; chaining .url() after transforms that changed the schema kind.","commonSituations":"Config/endpoint fields retyped from string to something else while keeping .url(); generic builders applying .url() to all 'location'-ish fields; copy-paste between URL and non-URL field definitions.","solutions":["Call .url() only on t.string() schemas","If the value is not a string, transform or retype the field to t.string().url()","Remove the .url() call if the field does not hold a URL"],"exampleFix":"// before\nt.object({ port: t.number().url() })\n// after\nt.object({ endpoint: t.string().url(), port: t.number() })","handlingStrategy":"type-guard","validationCode":"function canApplyUrl(schema: ZodLikeSchema<unknown>): boolean {\n  return schema.ir.k === \"string\";\n}\nif (!canApplyUrl(schema)) throw new Error(\"url() 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.url();\n} catch (err) {\n  if (err instanceof OmpTypeError && err.message.startsWith(\"cannot apply url\")) {\n    throw new Error(\"field must be t.string() to use .url()\");\n  } else throw err;\n}","preventionTips":["Chain .url() directly after t.string()","If a URL field can be absent, use t.string().url().optional() rather than a union before .url()","After changing a field's kind, remove constraint calls that no longer apply"],"tags":["schema-builder","type-mismatch","url"],"backgroundTag":"schema-method-kind-mismatch","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}