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.

simplify the parseUrlSchema

Kiran K6e16ea62932d
diff --git a/apps/web/lib/zod/schemas/groups.ts b/apps/web/lib/zod/schemas/groups.ts
index 37baf5f563..92a4a17228 100644
--- a/apps/web/lib/zod/schemas/groups.ts
+++ b/apps/web/lib/zod/schemas/groups.ts
@@ -4,7 +4,6 @@ import { validDomainRegex, validSlugRegex } from "@dub/utils";
import slugify from "@sindresorhus/slugify";
import { z } from "zod";
import { DiscountSchema } from "./discount";
-import { DESTINATION_URL_MAX_LENGTH } from "./links";
import { booleanQuerySchema, getPaginationQuerySchema } from "./misc";
import { RewardSchema } from "./rewards";
import { parseUrlSchema } from "./utils";
@@ -61,7 +60,7 @@ export const GroupSchemaExtended = GroupSchema.extend({
});
export const createOrUpdateDefaultLinkSchema = z.object({
- url: parseUrlSchema({ maxLength: DESTINATION_URL_MAX_LENGTH }),
+ url: parseUrlSchema,
});
export const createGroupSchema = z.object({
@@ -101,7 +100,7 @@ export const updateGroupSchema = createGroupSchema.partial().extend({
export const PartnerGroupDefaultLinkSchema = z.object({
id: z.string(),
domain: z.string(),
- url: parseUrlSchema({ maxLength: DESTINATION_URL_MAX_LENGTH }),
+ url: parseUrlSchema,
});
export const getGroupsQuerySchema = z
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index 7c39b21d59..d563965ccd 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -15,7 +15,7 @@ import {
} from "./utils";
export const getUrlQuerySchema = z.object({
- url: parseUrlSchema(),
+ url: parseUrlSchema,
});
export const getDomainQuerySchema = z.object({
@@ -223,7 +223,7 @@ export const domainKeySchema = z.object({
});
export const createLinkBodySchema = z.object({
- url: parseUrlSchemaAllowEmpty({ maxLength: DESTINATION_URL_MAX_LENGTH })
+ url: parseUrlSchemaAllowEmpty()
.describe("The destination URL of the short link.")
.openapi({
example: "https://google.com",
@@ -317,7 +317,7 @@ export const createLinkBodySchema = z.object({
.string()
.nullish()
.describe("The date and time when the short link will expire at."),
- expiredUrl: parseUrlSchema({ maxLength: DESTINATION_URL_MAX_LENGTH })
+ expiredUrl: parseUrlSchema
.nullish()
.describe("The URL to redirect to when the short link has expired."),
password: z
@@ -362,21 +362,18 @@ export const createLinkBodySchema = z.object({
.describe(
"Whether the short link uses link cloaking. Defaults to `false` if not provided.",
),
- ios: parseUrlSchema({ maxLength: DESTINATION_URL_MAX_LENGTH })
+ ios: parseUrlSchema
.nullish()
.describe(
"The iOS destination URL for the short link for iOS device targeting.",
),
- android: parseUrlSchema({ maxLength: DESTINATION_URL_MAX_LENGTH })
+ android: parseUrlSchema
.nullish()
.describe(
"The Android destination URL for the short link for Android device targeting.",
),
geo: z
- .record(
- z.string(),
- parseUrlSchema({ maxLength: DESTINATION_URL_MAX_LENGTH }),
- )
+ .record(z.string(), parseUrlSchema)
.nullish()
.describe(
"Geo targeting information for the short link in JSON format `{[COUNTRY]: https://example.com }`. See https://d.to/geo for more information.",
@@ -501,7 +498,7 @@ export const bulkUpdateLinksBodySchema = z.object({
})
.merge(
z.object({
- url: parseUrlSchema({ maxLength: DESTINATION_URL_MAX_LENGTH })
+ url: parseUrlSchema
.describe("The destination URL of the short link.")
.openapi({
example: "https://google.com",
diff --git a/apps/web/lib/zod/schemas/opens.ts b/apps/web/lib/zod/schemas/opens.ts
index e967885cfb..366548aa71 100644
--- a/apps/web/lib/zod/schemas/opens.ts
+++ b/apps/web/lib/zod/schemas/opens.ts
@@ -3,7 +3,7 @@ import { z } from "zod";
export const trackOpenRequestSchema = z
.object({
- deepLink: parseUrlSchema()
+ deepLink: parseUrlSchema
.optional()
.describe(
"The deep link that brought the user to the app. If left blank, Dub will fallback to probabilistic tracking by using the `dubDomain` parameter to check if there is an associated click event for the user's IP address. Learn more: https://d.to/ddl",
diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.ts
index de974ca6a9..e3fddc7791 100644
--- a/apps/web/lib/zod/schemas/partners.ts
+++ b/apps/web/lib/zod/schemas/partners.ts
@@ -13,7 +13,7 @@ import {
import { z } from "zod";
import { analyticsQuerySchema } from "./analytics";
import { analyticsResponse } from "./analytics-response";
-import { createLinkBodySchema, DESTINATION_URL_MAX_LENGTH } from "./links";
+import { createLinkBodySchema } from "./links";
import {
base64ImageSchema,
booleanQuerySchema,
@@ -529,7 +529,7 @@ export const createPartnerLinkSchema = z
.describe(
"The ID of the partner in your system. If both `partnerId` and `tenantId` are not provided, an error will be thrown.",
),
- url: parseUrlSchema({ maxLength: DESTINATION_URL_MAX_LENGTH })
+ url: parseUrlSchema
.describe(
"The URL to shorten (if not provided, the program's default URL will be used). Will throw an error if the domain doesn't match the program's default URL domain.",
)
@@ -551,7 +551,7 @@ export const createPartnerLinkSchema = z
export const upsertPartnerLinkSchema = createPartnerLinkSchema.merge(
z.object({
- url: parseUrlSchema({ maxLength: DESTINATION_URL_MAX_LENGTH }).describe(
+ url: parseUrlSchema.describe(
"The URL to upsert for. Will throw an error if the domain doesn't match the program's default URL domain.",
),
}),
diff --git a/apps/web/lib/zod/schemas/program-onboarding.ts b/apps/web/lib/zod/schemas/program-onboarding.ts
index 037f118c15..5a84cbc22c 100644
--- a/apps/web/lib/zod/schemas/program-onboarding.ts
+++ b/apps/web/lib/zod/schemas/program-onboarding.ts
@@ -1,7 +1,6 @@
import { PROGRAM_ONBOARDING_PARTNERS_LIMIT } from "@/lib/partners/constants";
import { PartnerLinkStructure, RewardStructure } from "@dub/prisma/client";
import { z } from "zod";
-import { DESTINATION_URL_MAX_LENGTH } from "./links";
import { maxDurationSchema } from "./misc";
import { updateProgramSchema } from "./programs";
import { parseUrlSchema } from "./utils";
@@ -11,7 +10,7 @@ export const programInfoSchema = z.object({
name: z.string().max(100),
logo: z.string(),
domain: z.string(),
- url: parseUrlSchema({ maxLength: DESTINATION_URL_MAX_LENGTH }).nullable(),
+ url: parseUrlSchema.nullable(),
linkStructure: z.nativeEnum(PartnerLinkStructure).default("short"),
linkParameter: z.string().nullish(),
});
diff --git a/apps/web/lib/zod/schemas/qr.ts b/apps/web/lib/zod/schemas/qr.ts
index e20212e109..02f61c36c0 100644
--- a/apps/web/lib/zod/schemas/qr.ts
+++ b/apps/web/lib/zod/schemas/qr.ts
@@ -9,7 +9,7 @@ import { booleanQuerySchema } from "./misc";
import { parseUrlSchema } from "./utils";
export const getQRCodeQuerySchema = z.object({
- url: parseUrlSchema().describe("The URL to generate a QR code for."),
+ url: parseUrlSchema.describe("The URL to generate a QR code for."),
logo: z
.string()
.optional()
diff --git a/apps/web/lib/zod/schemas/utils.ts b/apps/web/lib/zod/schemas/utils.ts
index 977dab86c0..c1641acff5 100644
--- a/apps/web/lib/zod/schemas/utils.ts
+++ b/apps/web/lib/zod/schemas/utils.ts
@@ -1,22 +1,17 @@
import z from "@/lib/zod";
import { getUrlFromString, isValidUrl, parseDateTime } from "@dub/utils";
+import { DESTINATION_URL_MAX_LENGTH } from "./links";
-export const parseUrlSchema = ({ maxLength }: { maxLength?: number } = {}) => {
- let schema = z.string();
-
- if (maxLength) {
- schema = schema.max(maxLength, {
- message: `Must be ${maxLength} or fewer characters long.`,
- });
- }
-
- return schema
- .transform((v) => getUrlFromString(v))
- .refine((v) => isValidUrl(v), { message: "Invalid URL" });
-};
+export const parseUrlSchema = z
+ .string()
+ .max(DESTINATION_URL_MAX_LENGTH, {
+ message: `Must be ${DESTINATION_URL_MAX_LENGTH} or fewer characters long.`,
+ })
+ .transform((v) => getUrlFromString(v))
+ .refine((v) => isValidUrl(v), { message: "Invalid URL" });
export const parseUrlSchemaAllowEmpty = ({
- maxLength,
+ maxLength = DESTINATION_URL_MAX_LENGTH,
trim = false,
}: {
maxLength?: number;
diff --git a/apps/web/lib/zod/schemas/webhooks.ts b/apps/web/lib/zod/schemas/webhooks.ts
index d129eb0546..96e08bb945 100644
--- a/apps/web/lib/zod/schemas/webhooks.ts
+++ b/apps/web/lib/zod/schemas/webhooks.ts
@@ -15,7 +15,7 @@ export const WebhookSchema = z.object({
export const createWebhookSchema = z.object({
name: z.string().min(1).max(40),
- url: parseUrlSchema(),
+ url: parseUrlSchema,
secret: z.string().optional(),
triggers: z.array(z.enum(WEBHOOK_TRIGGERS)),
linkIds: z.array(z.string()).optional(),