Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Increase UTM tag max length to 255

Kiran Kac098bf19d5e
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index 92955745ed..fe108ba4b1 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -19,6 +19,7 @@ import {
parseUrlSchema,
parseUrlSchemaAllowEmpty,
} from "./utils";
+import { utmTagInputSchema } from "./utm";
export const getUrlQuerySchema = z.object({
url: parseUrlSchema,
@@ -467,48 +468,24 @@ export const createLinkBodySchema = z.object({
.describe(
"Allow search engines to index your short link. Defaults to `false` if not provided. Learn more: https://d.to/noindex",
),
- utm_source: z
- .string()
- .transform((v) => (v === "" ? null : v))
- .nullish()
- .describe(
- "The UTM source of the short link. If set, this will populate or override the UTM source in the destination URL.",
- ),
- utm_medium: z
- .string()
- .transform((v) => (v === "" ? null : v))
- .nullish()
- .describe(
- "The UTM medium of the short link. If set, this will populate or override the UTM medium in the destination URL.",
- ),
- utm_campaign: z
- .string()
- .transform((v) => (v === "" ? null : v))
- .nullish()
- .describe(
- "The UTM campaign of the short link. If set, this will populate or override the UTM campaign in the destination URL.",
- ),
- utm_term: z
- .string()
- .transform((v) => (v === "" ? null : v))
- .nullish()
- .describe(
- "The UTM term of the short link. If set, this will populate or override the UTM term in the destination URL.",
- ),
- utm_content: z
- .string()
- .transform((v) => (v === "" ? null : v))
- .nullish()
- .describe(
- "The UTM content of the short link. If set, this will populate or override the UTM content in the destination URL.",
- ),
- ref: z
- .string()
- .transform((v) => (v === "" ? null : v))
- .nullish()
- .describe(
- "The referral tag of the short link. If set, this will populate or override the `ref` query parameter in the destination URL.",
- ),
+ utm_source: utmTagInputSchema.describe(
+ "The UTM source of the short link. If set, this will populate or override the UTM source in the destination URL.",
+ ),
+ utm_medium: utmTagInputSchema.describe(
+ "The UTM medium of the short link. If set, this will populate or override the UTM medium in the destination URL.",
+ ),
+ utm_campaign: utmTagInputSchema.describe(
+ "The UTM campaign of the short link. If set, this will populate or override the UTM campaign in the destination URL.",
+ ),
+ utm_term: utmTagInputSchema.describe(
+ "The UTM term of the short link. If set, this will populate or override the UTM term in the destination URL.",
+ ),
+ utm_content: utmTagInputSchema.describe(
+ "The UTM content of the short link. If set, this will populate or override the UTM content in the destination URL.",
+ ),
+ ref: utmTagInputSchema.describe(
+ "The referral tag of the short link. If set, this will populate or override the `ref` query parameter in the destination URL.",
+ ),
webhookIds: z
.array(z.string())
.nullish()
diff --git a/apps/web/lib/zod/schemas/utm.ts b/apps/web/lib/zod/schemas/utm.ts
index 24d751a7ca..ef0e92e6ff 100644
--- a/apps/web/lib/zod/schemas/utm.ts
+++ b/apps/web/lib/zod/schemas/utm.ts
@@ -1,49 +1,24 @@
import * as z from "zod/v4";
+export const UTM_TAG_MAX_LENGTH = 255;
+
+export const utmTagInputSchema = z
+ .string()
+ .trim()
+ .max(UTM_TAG_MAX_LENGTH)
+ .transform((v) => (v === "" ? null : v))
+ .nullish();
+
export const createUTMTemplateBodySchema = z.object({
name: z.string().trim().min(1, "UTM name is required").max(50),
- utm_source: z
- .string()
- .trim()
- .max(190)
- .transform((v) => (v === "" ? null : v))
- .nullish()
- .describe("The UTM source of the short link."),
- utm_medium: z
- .string()
- .trim()
- .max(190)
- .nullish()
- .transform((v) => (v === "" ? null : v))
- .describe("The UTM medium of the short link."),
- utm_campaign: z
- .string()
- .trim()
- .max(190)
- .nullish()
- .transform((v) => (v === "" ? null : v))
- .describe("The UTM campaign of the short link."),
- utm_term: z
- .string()
- .trim()
- .max(190)
- .nullish()
- .transform((v) => (v === "" ? null : v))
- .describe("The UTM term of the short link."),
- utm_content: z
- .string()
- .trim()
- .max(190)
- .nullish()
- .transform((v) => (v === "" ? null : v))
- .describe("The UTM content of the short link."),
- ref: z
- .string()
- .trim()
- .max(190)
- .nullish()
- .transform((v) => (v === "" ? null : v))
- .describe("The ref of the short link."),
+ utm_source: utmTagInputSchema.describe("The UTM source of the short link."),
+ utm_medium: utmTagInputSchema.describe("The UTM medium of the short link."),
+ utm_campaign: utmTagInputSchema.describe(
+ "The UTM campaign of the short link.",
+ ),
+ utm_term: utmTagInputSchema.describe("The UTM term of the short link."),
+ utm_content: utmTagInputSchema.describe("The UTM content of the short link."),
+ ref: utmTagInputSchema.describe("The ref of the short link."),
});
export const updateUTMTemplateBodySchema = createUTMTemplateBodySchema;
@@ -53,40 +28,22 @@ export const UTMTemplateSchema = z.object({
name: z.string(),
utm_source: z
.string()
- .trim()
- .max(190)
.nullish()
.describe("The UTM source of the short link."),
utm_medium: z
.string()
- .trim()
- .max(190)
.nullish()
.describe("The UTM medium of the short link."),
utm_campaign: z
.string()
- .trim()
- .max(190)
.nullish()
.describe("The UTM campaign of the short link."),
- utm_term: z
- .string()
- .trim()
- .max(190)
- .nullish()
- .describe("The UTM term of the short link."),
+ utm_term: z.string().nullish().describe("The UTM term of the short link."),
utm_content: z
.string()
- .trim()
- .max(190)
.nullish()
.describe("The UTM content of the short link."),
- ref: z
- .string()
- .trim()
- .max(190)
- .nullish()
- .describe("The ref of the short link."),
+ ref: z.string().max(190).nullish().describe("The ref of the short link."),
});
export const UTM_TAGS_PLURAL_LIST = [
diff --git a/apps/web/prisma/schema/link.prisma b/apps/web/prisma/schema/link.prisma
index ff2fa51e95..881a004bf4 100644
--- a/apps/web/prisma/schema/link.prisma
+++ b/apps/web/prisma/schema/link.prisma
@@ -17,11 +17,11 @@ model Link {
video String? @db.Text // OG video for the link
// UTM parameters
- utm_source String? // UTM source for the link (e.g. youtube.com)
- utm_medium String? // UTM medium for the link (e.g. social)
- utm_campaign String? // UTM campaign for the link (e.g. summer-sale)
- utm_term String? // UTM term for the link (e.g. dub)
- utm_content String? // UTM content for the link (e.g. description)
+ utm_source String? @db.VarChar(255) // UTM source for the link (e.g. youtube.com)
+ utm_medium String? @db.VarChar(255) // UTM medium for the link (e.g. social)
+ utm_campaign String? @db.VarChar(255) // UTM campaign for the link (e.g. summer-sale)
+ utm_term String? @db.VarChar(255) // UTM term for the link (e.g. dub)
+ utm_content String? @db.VarChar(255) // UTM content for the link (e.g. description)
rewrite Boolean @default(false) // whether to rewrite the link or not
diff --git a/apps/web/prisma/schema/utm.prisma b/apps/web/prisma/schema/utm.prisma
index afb462aac6..7ce31e81a7 100644
--- a/apps/web/prisma/schema/utm.prisma
+++ b/apps/web/prisma/schema/utm.prisma
@@ -3,12 +3,12 @@ model UtmTemplate {
name String
// Parameters
- utm_source String?
- utm_medium String?
- utm_campaign String?
- utm_term String?
- utm_content String?
- ref String?
+ utm_source String? @db.VarChar(255)
+ utm_medium String? @db.VarChar(255)
+ utm_campaign String? @db.VarChar(255)
+ utm_term String? @db.VarChar(255)
+ utm_content String? @db.VarChar(255)
+ ref String? @db.VarChar(255)
// User who created the template
user User? @relation(fields: [userId], references: [id])
diff --git a/apps/web/tests/links/create-link-error.test.ts b/apps/web/tests/links/create-link-error.test.ts
index 30c3969fcd..bcef43c619 100644
--- a/apps/web/tests/links/create-link-error.test.ts
+++ b/apps/web/tests/links/create-link-error.test.ts
@@ -60,6 +60,26 @@ const cases = [
},
},
},
+ {
+ name: "create link with utm_source exceeding max length",
+ body: {
+ domain,
+ url,
+ utm_source: "a".repeat(256),
+ },
+ expected: {
+ status: 422,
+ data: {
+ error: {
+ code: "unprocessable_entity",
+ message:
+ "too_big: utm_source: Too big: expected string to have <=255 characters",
+ doc_url:
+ "https://dub.co/docs/api-reference/errors#unprocessable-entity",
+ },
+ },
+ },
+ },
];
cases.forEach(({ name, body, expected }) => {
diff --git a/apps/web/tests/links/create-link.test.ts b/apps/web/tests/links/create-link.test.ts
index 2ee3d95c67..6956858084 100644
--- a/apps/web/tests/links/create-link.test.ts
+++ b/apps/web/tests/links/create-link.test.ts
@@ -250,6 +250,27 @@ describe.sequential("POST /links", async () => {
expect(LinkSchema.strict().parse(link)).toBeTruthy();
});
+ test("utm_source at max length", async () => {
+ const utmSource = "a".repeat(255);
+
+ onTestFinished(async () => {
+ await h.deleteLink(link.id);
+ });
+
+ const { status, data: link } = await http.post<Link>({
+ path: "/links",
+ body: {
+ url,
+ domain,
+ utm_source: utmSource,
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(link.utm_source).toBe(utmSource);
+ expect(LinkSchema.strict().parse(link)).toBeTruthy();
+ });
+
test("password protection", async () => {
const password = "link-password";