From dd28a8e7038078967fcc2aad8a6f6c05b97183a1 Mon Sep 17 00:00:00 2001 From: Mauricio Siu Date: Fri, 3 Apr 2026 22:40:49 -0600 Subject: [PATCH] feat(validation): centralize app name validation logic - Introduced `APP_NAME_REGEX` and `APP_NAME_MESSAGE` constants in `schema.ts` for consistent app name validation across `add-application.tsx`, `add-compose.tsx`, and `add-database.tsx`. - Updated regex and error message in the respective schemas to utilize the new constants, improving maintainability and readability. --- .../components/dashboard/project/add-application.tsx | 6 +++--- apps/dokploy/components/dashboard/project/add-compose.tsx | 6 +++--- apps/dokploy/components/dashboard/project/add-database.tsx | 6 +++--- apps/dokploy/utils/schema.ts | 4 ++++ 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/apps/dokploy/components/dashboard/project/add-application.tsx b/apps/dokploy/components/dashboard/project/add-application.tsx index 642c5bf0e..16fac353d 100644 --- a/apps/dokploy/components/dashboard/project/add-application.tsx +++ b/apps/dokploy/components/dashboard/project/add-application.tsx @@ -43,6 +43,7 @@ import { } from "@/components/ui/tooltip"; import { slugify } from "@/lib/slug"; import { api } from "@/utils/api"; +import { APP_NAME_MESSAGE, APP_NAME_REGEX } from "@/utils/schema"; const AddTemplateSchema = z.object({ name: z.string().min(1, { @@ -53,9 +54,8 @@ const AddTemplateSchema = z.object({ .min(1, { message: "App name is required", }) - .regex(/^[a-z](?!.*--)([a-z0-9-]*[a-z])?$/, { - message: - "App name supports lowercase letters, numbers, '-' and can only start and end letters, and does not support continuous '-'", + .regex(APP_NAME_REGEX, { + message: APP_NAME_MESSAGE, }), description: z.string().optional(), serverId: z.string().optional(), diff --git a/apps/dokploy/components/dashboard/project/add-compose.tsx b/apps/dokploy/components/dashboard/project/add-compose.tsx index 0687a7cab..0ecfb20ee 100644 --- a/apps/dokploy/components/dashboard/project/add-compose.tsx +++ b/apps/dokploy/components/dashboard/project/add-compose.tsx @@ -43,6 +43,7 @@ import { } from "@/components/ui/tooltip"; import { slugify } from "@/lib/slug"; import { api } from "@/utils/api"; +import { APP_NAME_MESSAGE, APP_NAME_REGEX } from "@/utils/schema"; const AddComposeSchema = z.object({ composeType: z.enum(["docker-compose", "stack"]).optional(), @@ -54,9 +55,8 @@ const AddComposeSchema = z.object({ .min(1, { message: "App name is required", }) - .regex(/^[a-z](?!.*--)([a-z0-9-]*[a-z0-9])?$/, { - message: - "App name supports lowercase letters, numbers, '-' and must start with a letter, end with a letter or number, and cannot contain consecutive '-'", + .regex(APP_NAME_REGEX, { + message: APP_NAME_MESSAGE, }), description: z.string().optional(), serverId: z.string().optional(), diff --git a/apps/dokploy/components/dashboard/project/add-database.tsx b/apps/dokploy/components/dashboard/project/add-database.tsx index a3d85304a..1170e3622 100644 --- a/apps/dokploy/components/dashboard/project/add-database.tsx +++ b/apps/dokploy/components/dashboard/project/add-database.tsx @@ -52,6 +52,7 @@ import { } from "@/components/ui/tooltip"; import { slugify } from "@/lib/slug"; import { api } from "@/utils/api"; +import { APP_NAME_MESSAGE, APP_NAME_REGEX } from "@/utils/schema"; type DbType = z.infer["type"]; @@ -82,9 +83,8 @@ const baseDatabaseSchema = z.object({ .min(1, { message: "App name is required", }) - .regex(/^[a-z](?!.*--)([a-z0-9-]*[a-z])?$/, { - message: - "App name supports lowercase letters, numbers, '-' and can only start and end letters, and does not support continuous '-'", + .regex(APP_NAME_REGEX, { + message: APP_NAME_MESSAGE, }), databasePassword: z .string() diff --git a/apps/dokploy/utils/schema.ts b/apps/dokploy/utils/schema.ts index 73df59d6f..addbbc344 100644 --- a/apps/dokploy/utils/schema.ts +++ b/apps/dokploy/utils/schema.ts @@ -10,6 +10,10 @@ if (typeof window === "undefined") { })(); } +export const APP_NAME_REGEX = /^[a-z](?!.*--)([a-z0-9-]*[a-z0-9])?$/; +export const APP_NAME_MESSAGE = + "App name supports lowercase letters, numbers, '-' and must start with a letter, end with a letter or number, and cannot contain consecutive '-'"; + export const uploadFileSchema = zfd.formData({ applicationId: z.string().optional(), zip: zfd.file(),