From 8fb9d4d155aa4346c49c9f92568258b4b303c835 Mon Sep 17 00:00:00 2001 From: MichaelUnkey <148160799+MichaelUnkey@users.noreply.github.com> Date: Mon, 28 Oct 2024 09:22:19 -0400 Subject: [PATCH] fix: trim spaces in strings for names (#2527) * trim names in dashboard * [autofix.ci] apply automated fixes * missed changes * [autofix.ci] apply automated fixes * Changed method for trim * [autofix.ci] apply automated fixes * missed min max --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- .../[keyAuthId]/[keyId]/settings/update-key-name.tsx | 1 + .../[keyId]/settings/update-key-owner-id.tsx | 1 + .../apis/[apiId]/keys/[keyAuthId]/new/client.tsx | 5 +++-- .../(app)/apis/[apiId]/settings/update-api-name.tsx | 2 +- apps/dashboard/app/(app)/apis/create-api-button.tsx | 2 +- .../authorization/roles/[roleId]/update-role.tsx | 8 +++++++- .../[namespaceId]/overrides/create-new-override.tsx | 6 +++++- .../[namespaceId]/settings/update-namespace-name.tsx | 12 ++++++++++-- .../app/(app)/ratelimits/create-namespace-button.tsx | 10 +++++++++- .../(app)/settings/general/update-workspace-name.tsx | 2 +- apps/dashboard/app/new/create-api.tsx | 2 +- apps/dashboard/app/new/create-workspace.tsx | 2 +- 12 files changed, 41 insertions(+), 12 deletions(-) diff --git a/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/[keyId]/settings/update-key-name.tsx b/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/[keyId]/settings/update-key-name.tsx index 188e5b72ad..a546cd047b 100644 --- a/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/[keyId]/settings/update-key-name.tsx +++ b/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/[keyId]/settings/update-key-name.tsx @@ -24,6 +24,7 @@ const formSchema = z.object({ keyId: z.string(), name: z .string() + .trim() .transform((e) => (e === "" ? undefined : e)) .optional(), }); diff --git a/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/[keyId]/settings/update-key-owner-id.tsx b/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/[keyId]/settings/update-key-owner-id.tsx index fcf3a5211c..d635c63673 100644 --- a/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/[keyId]/settings/update-key-owner-id.tsx +++ b/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/[keyId]/settings/update-key-owner-id.tsx @@ -24,6 +24,7 @@ const formSchema = z.object({ keyId: z.string(), ownerId: z .string() + .trim() .transform((e) => (e === "" ? undefined : e)) .optional(), }); diff --git a/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/new/client.tsx b/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/new/client.tsx index f940d81724..f6607c626c 100644 --- a/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/new/client.tsx +++ b/apps/dashboard/app/(app)/apis/[apiId]/keys/[keyAuthId]/new/client.tsx @@ -57,10 +57,11 @@ const formSchema = z.object({ .default(16), prefix: z .string() + .trim() .max(8, { message: "Please limit the prefix to under 8 characters." }) .optional(), - ownerId: z.string().optional(), - name: z.string().optional(), + ownerId: z.string().trim().optional(), + name: z.string().trim().optional(), metaEnabled: z.boolean().default(false), meta: z .string() diff --git a/apps/dashboard/app/(app)/apis/[apiId]/settings/update-api-name.tsx b/apps/dashboard/app/(app)/apis/[apiId]/settings/update-api-name.tsx index f7878ae245..1711664eea 100644 --- a/apps/dashboard/app/(app)/apis/[apiId]/settings/update-api-name.tsx +++ b/apps/dashboard/app/(app)/apis/[apiId]/settings/update-api-name.tsx @@ -21,7 +21,7 @@ import { z } from "zod"; import { tags } from "@/lib/cache"; import { revalidateTag } from "../../../../actions"; const formSchema = z.object({ - name: z.string(), + name: z.string().trim().min(3, "Name is required and should be at least 3 characters"), apiId: z.string(), workspaceId: z.string(), }); diff --git a/apps/dashboard/app/(app)/apis/create-api-button.tsx b/apps/dashboard/app/(app)/apis/create-api-button.tsx index 851e1e0289..5d5af2fa6e 100644 --- a/apps/dashboard/app/(app)/apis/create-api-button.tsx +++ b/apps/dashboard/app/(app)/apis/create-api-button.tsx @@ -23,7 +23,7 @@ import { useForm } from "react-hook-form"; import { z } from "zod"; const formSchema = z.object({ - name: z.string().min(2).max(50), + name: z.string().trim().min(3, "Name must be at least 3 characters long").max(50), }); export const CreateApiButton = ({ ...rest }: React.ButtonHTMLAttributes) => { diff --git a/apps/dashboard/app/(app)/authorization/roles/[roleId]/update-role.tsx b/apps/dashboard/app/(app)/authorization/roles/[roleId]/update-role.tsx index e760a75e7b..4436814e76 100644 --- a/apps/dashboard/app/(app)/authorization/roles/[roleId]/update-role.tsx +++ b/apps/dashboard/app/(app)/authorization/roles/[roleId]/update-role.tsx @@ -38,7 +38,13 @@ type Props = { }; const formSchema = z.object({ - name: z.string(), + name: z + .string() + .min(3) + .regex(/^[a-zA-Z0-9_:\-\.\*]+$/, { + message: + "Must be at least 3 characters long and only contain alphanumeric, colons, periods, dashes and underscores", + }), description: z.string().optional(), }); diff --git a/apps/dashboard/app/(app)/ratelimits/[namespaceId]/overrides/create-new-override.tsx b/apps/dashboard/app/(app)/ratelimits/[namespaceId]/overrides/create-new-override.tsx index 6f90e18c43..5ba0a17612 100644 --- a/apps/dashboard/app/(app)/ratelimits/[namespaceId]/overrides/create-new-override.tsx +++ b/apps/dashboard/app/(app)/ratelimits/[namespaceId]/overrides/create-new-override.tsx @@ -28,7 +28,11 @@ import { useForm } from "react-hook-form"; import { z } from "zod"; const formSchema = z.object({ - identifier: z.string().min(2).max(250), + identifier: z + .string() + .trim() + .min(3, "Name is required and should be at least 3 characters") + .max(250), limit: z.coerce.number().int().min(1).max(10_000), duration: z.coerce .number() diff --git a/apps/dashboard/app/(app)/ratelimits/[namespaceId]/settings/update-namespace-name.tsx b/apps/dashboard/app/(app)/ratelimits/[namespaceId]/settings/update-namespace-name.tsx index b0bcf3eb61..5d24f2bc6f 100644 --- a/apps/dashboard/app/(app)/ratelimits/[namespaceId]/settings/update-namespace-name.tsx +++ b/apps/dashboard/app/(app)/ratelimits/[namespaceId]/settings/update-namespace-name.tsx @@ -20,7 +20,15 @@ import { useRouter } from "next/navigation"; import { useForm } from "react-hook-form"; import { z } from "zod"; const formSchema = z.object({ - name: z.string(), + name: z + .string() + .trim() + .min(3) + .max(50) + .regex(/^[a-zA-Z0-9_\-\.]+$/, { + message: + "Name must be 3-50 characters long and can only contain letters, numbers, underscores, hyphens, and periods.", + }), namespaceId: z.string(), workspaceId: z.string(), }); @@ -56,7 +64,7 @@ export const UpdateNamespaceName: React.FC = ({ namespace }) => { }); async function onSubmit(values: z.infer) { if (values.name === namespace.name || !values.name) { - return toast.error("Please provide a valid name before saving."); + return toast.error("Please provide a different name before saving."); } await updateName.mutateAsync(values); } diff --git a/apps/dashboard/app/(app)/ratelimits/create-namespace-button.tsx b/apps/dashboard/app/(app)/ratelimits/create-namespace-button.tsx index 3764a52a30..0f0330d73a 100644 --- a/apps/dashboard/app/(app)/ratelimits/create-namespace-button.tsx +++ b/apps/dashboard/app/(app)/ratelimits/create-namespace-button.tsx @@ -22,7 +22,15 @@ import { useForm } from "react-hook-form"; import { z } from "zod"; const formSchema = z.object({ - name: z.string().regex(/^[a-zA-Z0-9_\-\.]{3,50}$/), + name: z + .string() + .trim() + .min(1, "Name must not be empty") + .max(50, "Name must not exceed 50 characters") + .regex( + /^[a-zA-Z0-9_\-\.]+$/, + "Only alphanumeric characters, underscores, hyphens, and periods are allowed", + ), }); export const CreateNamespaceButton = ({ diff --git a/apps/dashboard/app/(app)/settings/general/update-workspace-name.tsx b/apps/dashboard/app/(app)/settings/general/update-workspace-name.tsx index 70a0be2673..50c47cb5e2 100644 --- a/apps/dashboard/app/(app)/settings/general/update-workspace-name.tsx +++ b/apps/dashboard/app/(app)/settings/general/update-workspace-name.tsx @@ -18,7 +18,7 @@ const validCharactersRegex = /^[a-zA-Z0-9-_]+$/; const formSchema = z.object({ workspaceId: z.string(), - name: z.string().min(3).regex(validCharactersRegex, { + name: z.string().trim().min(3).regex(validCharactersRegex, { message: "Workspace can only contain letters, numbers, dashes, and underscores", }), }); diff --git a/apps/dashboard/app/new/create-api.tsx b/apps/dashboard/app/new/create-api.tsx index e1180af3a2..5a8b572a0d 100644 --- a/apps/dashboard/app/new/create-api.tsx +++ b/apps/dashboard/app/new/create-api.tsx @@ -22,7 +22,7 @@ import { useRouter } from "next/navigation"; import { useForm } from "react-hook-form"; import { z } from "zod"; const formSchema = z.object({ - name: z.string().min(3, "Name is required and should be at least 3 characters").max(50), + name: z.string().trim().min(3, "Name is required and should be at least 3 characters").max(50), }); type Props = { diff --git a/apps/dashboard/app/new/create-workspace.tsx b/apps/dashboard/app/new/create-workspace.tsx index 79ac2fa27d..0500e22b36 100644 --- a/apps/dashboard/app/new/create-workspace.tsx +++ b/apps/dashboard/app/new/create-workspace.tsx @@ -23,7 +23,7 @@ import { useForm } from "react-hook-form"; import { z } from "zod"; const formSchema = z.object({ - name: z.string().min(3, "Name is required and should be at least 3 characters").max(50), + name: z.string().trim().min(3, "Name is required and should be at least 3 characters").max(50), }); export const CreateWorkspace: React.FC = () => {