From d93b9762ed1c879224a2e466140bf9de594cc003 Mon Sep 17 00:00:00 2001 From: MTG2000 Date: Thu, 16 Jun 2022 17:26:02 +0300 Subject: [PATCH] feat: add validation to profile/editAbout --- .../Components/StoryForm/StoryForm.tsx | 6 +-- .../ProfilePage/AboutCard/UpdateAboutForm.tsx | 39 ++++++++++++++++++- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/features/Posts/pages/CreatePostPage/Components/StoryForm/StoryForm.tsx b/src/features/Posts/pages/CreatePostPage/Components/StoryForm/StoryForm.tsx index 3dcb55b..63d8c66 100644 --- a/src/features/Posts/pages/CreatePostPage/Components/StoryForm/StoryForm.tsx +++ b/src/features/Posts/pages/CreatePostPage/Components/StoryForm/StoryForm.tsx @@ -30,9 +30,9 @@ const FileSchema = yup.lazy((value: string | File[]) => { }) const schema = yup.object({ - title: yup.string().required().min(10), - tags: yup.array().required().min(1), - body: yup.string().required().min(50, 'you have to write at least 10 words'), + title: yup.string().trim().required().min(10, 'the title is too short'), + tags: yup.array().required().min(1, 'please pick at least one relevant tag'), + body: yup.string().required().min(50, 'stories should have a minimum of 10 words'), cover_image: yup.array().of(FileSchema as any) }).required(); diff --git a/src/features/Profiles/pages/ProfilePage/AboutCard/UpdateAboutForm.tsx b/src/features/Profiles/pages/ProfilePage/AboutCard/UpdateAboutForm.tsx index 5dc6ab8..4f06cb1 100644 --- a/src/features/Profiles/pages/ProfilePage/AboutCard/UpdateAboutForm.tsx +++ b/src/features/Profiles/pages/ProfilePage/AboutCard/UpdateAboutForm.tsx @@ -4,6 +4,8 @@ import Button from "src/Components/Button/Button"; import { useUpdateProfileAboutMutation } from "src/graphql"; import { NotificationsService } from "src/services/notifications.service"; import AboutCard from "./AboutCard" +import * as yup from "yup"; +import { yupResolver } from "@hookform/resolvers/yup"; interface Props { data: ComponentProps['user'], @@ -12,10 +14,45 @@ interface Props { type IFormInputs = Props['data']; +const schema: yup.SchemaOf = yup.object({ + name: yup.string().trim().required().min(2), + avatar: yup.string().url().required(), + bio: yup.string().ensure(), + email: yup.string().email().ensure(), + github: yup.string().ensure(), + jobTitle: yup.string().ensure(), + lightning_address: yup + .string() + .test({ + name: "is valid lightning_address", + test: async value => { + try { + if (value) { + const [name, domain] = value.split("@"); + const lnurl = `https://${domain}/.well-known/lnurlp/${name}`; + await fetch(lnurl); + } + return true; + } catch (error) { + return false; + } + } + }) + .ensure() + .label("lightning address"), + linkedin: yup.string().ensure(), + location: yup.string().ensure(), + twitter: yup.string().ensure(), + website: yup.string().url().ensure(), + +}).required(); + export default function UpdateAboutForm({ data, onClose }: Props) { const { register, formState: { errors }, handleSubmit } = useForm({ - defaultValues: data + defaultValues: data, + resolver: yupResolver(schema), + mode: 'onBlur', }); const [mutate, mutationStatus] = useUpdateProfileAboutMutation({