From 195b2929e0243a3c03a4dfa237e534e40e5ea4e8 Mon Sep 17 00:00:00 2001 From: soup Date: Mon, 10 Apr 2023 14:36:13 +0200 Subject: [PATCH] feat(filters): improve RegexField validation (#803) * make sure fields are validated on page load * make red border only show if useRegex enabled * ensure tooltips have higher z-index in RegexField removed z-index in customtooltip.tsx as its causing issues with tooltips in other components * improved regex validation return false for cases that are unsupported by Go * improved check for unsupported conditionals thanks nuxen --- web/src/components/inputs/input.tsx | 58 ++++++++++++++++--- web/src/components/tooltips/CustomTooltip.tsx | 2 +- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/web/src/components/inputs/input.tsx b/web/src/components/inputs/input.tsx index 367022e..df2de47 100644 --- a/web/src/components/inputs/input.tsx +++ b/web/src/components/inputs/input.tsx @@ -1,8 +1,9 @@ -import { Field, FieldProps } from "formik"; +import { Field, FieldProps, useFormikContext } from "formik"; import { classNames } from "../../utils"; import { EyeIcon, EyeSlashIcon, CheckCircleIcon, XCircleIcon } from "@heroicons/react/24/solid"; import { useToggle } from "../../hooks/hooks"; import { CustomTooltip } from "../tooltips/CustomTooltip"; +import { useEffect } from "react"; type COL_WIDTHS = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12; @@ -100,16 +101,52 @@ export const RegexField = ({ tooltip, disabled }: RegexFieldProps) => { - const golangRegex = /^((\\\*|\\\?|\\[^\s\\])+|\(\?i\))(\|((\\\*|\\\?|\\[^\s\\])+|\(\?i\)))*$/; - const validRegex = (pattern: string) => { + + // Check for unsupported lookahead and lookbehind assertions + if (/\(\?<=|\(\?/.test(pattern)) { + return false; + } + + // Check for unsupported recursive patterns + if (/\(\?(R|0)\)/.test(pattern)) { + return false; + } + + // Check for unsupported possessive quantifiers + if (/[*+?]{1}\+|\{[0-9]+,[0-9]*\}\+/.test(pattern)) { + return false; + } + + // Check for unsupported control verbs + if (/\\g { let error = ""; @@ -121,6 +158,13 @@ export const RegexField = ({ return error; }; + const { validateForm } = useFormikContext(); + useEffect(() => { + if (useRegex) { + validateForm(); + } + }, [useRegex]); + return (
{label} - {tooltip && {tooltip}} + {tooltip && {tooltip}}
)} @@ -152,7 +196,7 @@ export const RegexField = ({ defaultValue={defaultValue} autoComplete={autoComplete} className={classNames( - meta.touched && meta.error + useRegex && meta.error ? "focus:ring-red-500 focus:border-red-500 border-red-500" : "focus:ring-blue-500 dark:focus:ring-blue-500 focus:border-blue-500 dark:focus:border-blue-500 border-gray-300 dark:border-gray-700", disabled @@ -166,7 +210,7 @@ export const RegexField = ({ {useRegex && (
- {meta.touched && !meta.error ? ( + {!meta.error ? (