From 890669741c07fefdff0037f695151abf74215955 Mon Sep 17 00:00:00 2001 From: Jorik Schellekens Date: Tue, 1 Sep 2020 09:57:49 +0200 Subject: [PATCH] Remove Yup dependency and fix link validation --- package.json | 2 +- src/components/CreateLinkTile.tsx | 36 ++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index b4af7ab..792a413 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "react": "^16.13.1", "react-dom": "^16.13.1", "react-scripts": "3.4.1", - "yup": "^0.29.1" + "zod": "^1.10.3" }, "scripts": { "start": "react-scripts start", diff --git a/src/components/CreateLinkTile.tsx b/src/components/CreateLinkTile.tsx index 63a57d3..06b0c97 100644 --- a/src/components/CreateLinkTile.tsx +++ b/src/components/CreateLinkTile.tsx @@ -15,12 +15,15 @@ limitations under the License. */ import React, { useEffect, useRef } from 'react'; +import { Formik, Form } from 'formik'; + import Tile from './Tile'; import Button from './Button'; import TextButton from './TextButton'; import Input from './Input'; -import { Formik, Form } from 'formik'; -import * as Yup from 'yup'; + +import { parseHash } from '../parser/parser'; +import { LinkKind } from '../parser/types'; import './CreateLinkTile.scss'; @@ -28,6 +31,24 @@ interface ILinkNotCreatedTileProps { setLink: React.Dispatch>; } +interface FormValues { + identifier: string; +} + +// Hacky use of types here +function validate(values: FormValues): Partial { + const errors: Partial = {}; + + const parse = parseHash(values.identifier); + + if (parse.kind === LinkKind.ParseFailed) { + errors.identifier = + "That link doesn't look right. Double check the details."; + } + + return errors; +} + const LinkNotCreatedTile: React.FC = ( props: ILinkNotCreatedTileProps ) => { @@ -41,15 +62,7 @@ const LinkNotCreatedTile: React.FC = ( initialValues={{ identifier: '', }} - validationSchema={Yup.object({ - identifier: Yup.string() - .test( - 'is-identifier', - "That link doesn't look right. Double check the details.", - (link) => link - ) - .required('Required'), - })} + validate={validate} onSubmit={(values): void => { props.setLink( document.location.protocol + @@ -109,7 +122,6 @@ const LinkCreatedTile: React.FC = (props) => { const CreateLinkTile: React.FC = () => { const [link, setLink] = React.useState(''); - console.log(link); if (!link) { return ; } else {