From e9e48d5aae28f94d2ed2fadfb019be50854ffe37 Mon Sep 17 00:00:00 2001 From: keyan Date: Thu, 10 Mar 2022 16:00:04 -0600 Subject: [PATCH] better regex for url validation --- components/link-form.js | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/components/link-form.js b/components/link-form.js index 26bbadee..3dd6990d 100644 --- a/components/link-form.js +++ b/components/link-form.js @@ -2,7 +2,6 @@ import { Form, Input, SubmitButton } from '../components/form' import { useRouter } from 'next/router' import * as Yup from 'yup' import { gql, useLazyQuery, useMutation } from '@apollo/client' -import { ensureProtocol } from '../lib/url' import ActionTooltip from '../components/action-tooltip' import Countdown from './countdown' import AdvPostForm, { AdvPostInitial, AdvPostSchema } from './adv-post-form' @@ -10,21 +9,11 @@ import { ITEM_FIELDS } from '../fragments/items' import Item from './item' import AccordianItem from './accordian-item' +// eslint-disable-next-line +const URL = /^((https?|ftp):\/\/)?(www.)?(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i export const LinkSchema = Yup.object({ title: Yup.string().required('required').trim(), - url: Yup.string().test({ - name: 'url', - test: (value) => { - try { - value = ensureProtocol(value) - const valid = new URL(value) - return Boolean(valid) - } catch { - return false - } - }, - message: 'invalid url' - }).required('required'), + url: Yup.string().matches(URL, 'invalid url').required('required'), ...AdvPostSchema })