From 3889b49fa1312bb6d573f52bd3769feeca6be9b7 Mon Sep 17 00:00:00 2001 From: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Date: Fri, 18 Nov 2022 00:26:47 +0530 Subject: [PATCH] fix: use zod for validation (#5498) * fix: use zod for validation * fix: use slug instead of url Co-authored-by: Peer Richelsen --- .../ee/teams/pages/team-profile-view.tsx | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/packages/features/ee/teams/pages/team-profile-view.tsx b/packages/features/ee/teams/pages/team-profile-view.tsx index 3e7cb10b04..e800b64083 100644 --- a/packages/features/ee/teams/pages/team-profile-view.tsx +++ b/packages/features/ee/teams/pages/team-profile-view.tsx @@ -1,8 +1,10 @@ +import { zodResolver } from "@hookform/resolvers/zod"; import { MembershipRole, Prisma } from "@prisma/client"; import { useSession } from "next-auth/react"; import Link from "next/link"; import { useRouter } from "next/router"; import { Controller, useForm } from "react-hook-form"; +import { z } from "zod"; import { IS_TEAM_BILLING_ENABLED, WEBAPP_URL } from "@calcom/lib/constants"; import { getPlaceholderAvatar } from "@calcom/lib/getPlaceholderAvatar"; @@ -19,12 +21,19 @@ import Meta from "@calcom/ui/v2/core/Meta"; import { getLayout } from "@calcom/ui/v2/core/layouts/SettingsLayout"; import showToast from "@calcom/ui/v2/core/notifications"; -interface TeamProfileValues { - name: string; - url: string; - logo: string; - bio: string; -} +const regex = new RegExp("^[a-zA-Z0-9-]*$"); + +const teamProfileFormSchema = z.object({ + name: z.string(), + slug: z + .string() + .regex(regex, { + message: "Url can only have alphanumeric characters(a-z, 0-9) and hyphen(-) symbol.", + }) + .min(1, { message: "Url cannot be left empty" }), + logo: z.string(), + bio: z.string(), +}); const ProfileView = () => { const { t } = useLocale(); @@ -42,7 +51,9 @@ const ProfileView = () => { }, }); - const form = useForm(); + const form = useForm({ + resolver: zodResolver(teamProfileFormSchema), + }); const { data: team, isLoading } = trpc.viewer.teams.get.useQuery( { teamId: Number(router.query.id) }, @@ -53,11 +64,11 @@ const ProfileView = () => { onSuccess: (team) => { if (team) { form.setValue("name", team.name || ""); - form.setValue("url", team.slug || ""); + form.setValue("slug", team.slug || ""); form.setValue("logo", team.logo || ""); form.setValue("bio", team.bio || ""); if (team.slug === null && (team?.metadata as Prisma.JsonObject)?.requestedSlug) { - form.setValue("url", ((team?.metadata as Prisma.JsonObject)?.requestedSlug as string) || ""); + form.setValue("slug", ((team?.metadata as Prisma.JsonObject)?.requestedSlug as string) || ""); } } }, @@ -124,7 +135,7 @@ const ProfileView = () => { const variables = { logo: values.logo, name: values.name, - slug: values.url, + slug: values.slug, bio: values.bio, }; objectKeys(variables).forEach((key) => { @@ -176,16 +187,17 @@ const ProfileView = () => { /> (
{ - form.setValue("url", e?.target.value); + form.clearErrors("slug"); + form.setValue("slug", e?.target.value); }} />