From 3dab4261fb88ee61866400e55a0cdd8d81ac8bc2 Mon Sep 17 00:00:00 2001 From: Shaun Hamilton Date: Tue, 21 Mar 2023 08:47:04 +0000 Subject: [PATCH] fix(client): allow endpoint on projects (#49656) * fix(client): allow endpoint on projects * refactor: structure form.tsx code similarly Co-authored-by: Oliver Eyton-Williams --- .../components/formHelpers/form-fields.tsx | 20 ++++++++++++------- client/src/components/formHelpers/form.tsx | 18 +++++++++++------ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/client/src/components/formHelpers/form-fields.tsx b/client/src/components/formHelpers/form-fields.tsx index fdda540cca9..0ab9eeb9931 100644 --- a/client/src/components/formHelpers/form-fields.tsx +++ b/client/src/components/formHelpers/form-fields.tsx @@ -59,13 +59,19 @@ function FormFields(props: FormFieldsProps): JSX.Element { validationError = (err as { message?: string })?.message; } } - const validationWarning = composeValidators( - name === 'githubLink' || isEditorLinkAllowed ? null : editorValidator, - fCCValidator, - httpValidator, - isLocalLinkAllowed ? null : localhostValidator, - pathValidator - )(value); + + const validators = [fCCValidator, httpValidator]; + const isSolutionLink = name !== 'githubLink'; + if (isSolutionLink && !isEditorLinkAllowed) { + validators.push(editorValidator); + if (isLocalLinkAllowed) { + validators.push(pathValidator); + } + } + if (!isLocalLinkAllowed) { + validators.push(localhostValidator); + } + const validationWarning = composeValidators(...validators)(value); const message: string = (error || validationError || validationWarning) as string; diff --git a/client/src/components/formHelpers/form.tsx b/client/src/components/formHelpers/form.tsx index 516ac60e941..dc5ecd0c894 100644 --- a/client/src/components/formHelpers/form.tsx +++ b/client/src/components/formHelpers/form.tsx @@ -42,13 +42,19 @@ function formatUrlValues( invalidValues: [] }; const urlValues = Object.keys(values).reduce((result, key: string) => { + // NOTE: pathValidator is not used here, because it is only used as a + // suggestion - should not prevent form submission + const validators = [fCCValidator, httpValidator]; + const isSolutionLink = key !== 'githubLink'; + if (isSolutionLink && !isEditorLinkAllowed) { + validators.push(editorValidator); + } + if (!isLocalLinkAllowed) { + validators.push(localhostValidator); + } + let value: string = values[key]; - const nullOrWarning: JSX.Element | null = composeValidators( - fCCValidator, - httpValidator, - isLocalLinkAllowed ? null : localhostValidator, - key === 'githubLink' || isEditorLinkAllowed ? null : editorValidator - )(value); + const nullOrWarning = composeValidators(...validators)(value); if (nullOrWarning) { validatedValues.invalidValues.push(nullOrWarning); }