Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(EditGuildDrawer): validate eventSources with Zod #1419

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/components/[guild]/EditGuild/EditGuildDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
useDisclosure,
} from "@chakra-ui/react"
import { GuildContact, Schemas } from "@guildxyz/types"
import { zodResolver } from "@hookform/resolvers/zod"
import UrlName from "components/[guild]/EditGuild/components/UrlName"
import { useThemeContext } from "components/[guild]/ThemeContext"
import useGuild from "components/[guild]/hooks/useGuild"
Expand All @@ -39,6 +40,7 @@ import { FormProvider, useForm } from "react-hook-form"
import { EventSourcesKey, GuildTags } from "types"
import handleSubmitDirty from "utils/handleSubmitDirty"
import { Chain } from "wagmiConfig/chains"
import { z } from "zod"
import LeaveButton from "../LeaveButton"
import useGuildPermission from "../hooks/useGuildPermission"
import useUser from "../hooks/useUser"
Expand All @@ -60,6 +62,33 @@ type Props = {
onClose: () => void
}

const eventSourcesSchema = z
.object({
EVENTBRITE: z
.string()
.url()
.regex(/(.)+eventbrite\.com\/e\/(.)+/)
.transform((url) => url.replace(/\/+$/, ""))
.optional()
.nullable(),
LUMA: z
.string()
.url()
.regex(/(.)+lu\.ma\/(u|user)\/(.)+/)
.transform((url) => url.replace(/\/+$/, ""))
.optional()
.nullable(),
LINK3: z
.string()
.url()
.regex(/(.)+link3\.to\/(.)+/)
.transform((url) => url.replace(/\/+$/, ""))
.optional()
.nullable(),
DISCORD: z.string().url().optional().nullable(),
})
.optional()

const DynamicFeatureFlags = dynamic(() => import("./components/FeatureFlags"))

export type EditGuildForm = Schemas["GuildUpdatePayload"] & {
Expand Down Expand Up @@ -130,6 +159,13 @@ const EditGuildDrawer = ({
const methods = useForm<EditGuildForm>({
mode: "all",
defaultValues,
resolver: zodResolver(
z
.object({
eventSources: eventSourcesSchema,
})
.passthrough() // So we don't validate the other keys with Zod (yet)
),
})
const { control, reset, formState } = methods

Expand Down
15 changes: 3 additions & 12 deletions src/components/[guild]/EditGuild/components/Events/EventInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const eventSourceNames: Record<EventSourcesKey, string> = {
EVENTBRITE: "Eventbrite",
LINK3: "Link3",
LUMA: "lu.ma",
DISCORD: "Diiscord",
DISCORD: "Discord",
}

const logos: Record<EventSourcesKey, string> = {
Expand All @@ -33,19 +33,10 @@ const logos: Record<EventSourcesKey, string> = {

const placeholders: Record<Exclude<EventSourcesKey, "DISCORD">, string> = {
EVENTBRITE: "https://www.eventbrite.com/e/...",
LUMA: "https://lu.ma/u/...",
LUMA: "https://lu.ma/user/...",
LINK3: "https://link3.to/e/...",
}

const validators: Record<
Exclude<EventSourcesKey, "DISCORD">,
(value?: string) => true | string
> = {
EVENTBRITE: (url) => !!url.match(/(.)+eventbrite\.com\/e\/(.)+/) || "Invalid URL",
LUMA: (url) => !!url.match(/(.)+lu\.ma\/u\/(.)+/) || "Invalid URL",
LINK3: (url) => !!url.match(/(.)+link3\.to\/(.)+/) || "Invalid URL",
}

const EventInput = ({ eventSource }: Props) => {
const {
register,
Expand All @@ -70,7 +61,6 @@ const EventInput = ({ eventSource }: Props) => {
<Input
{...register(`eventSources.${eventSource}`, {
required: "This field is required.",
validate: validators[eventSource],
})}
size={"lg"}
placeholder={placeholders[eventSource]}
Expand All @@ -83,6 +73,7 @@ const EventInput = ({ eventSource }: Props) => {
onClick={() =>
setValue(`eventSources.${eventSource}`, undefined, {
shouldDirty: true,
shouldValidate: true,
})
}
/>
Expand Down
Loading