From 5736aff9e5b5efe031e9650b3c9c837dc2846b6b Mon Sep 17 00:00:00 2001 From: Iukou Siarhei <45054016+BlazarQSO@users.noreply.github.com> Date: Fri, 20 Dec 2024 12:12:50 +0300 Subject: [PATCH] EPMRPP-97895 || Add validation on entered URL (#77) * EPMRPP-97895 || Add validation on entered URL * EPMRPP-97895 || Code Review fix - 1 --- .../components/{constans.js => constants.js} | 0 .../integrationFormFields.jsx | 7 ++++--- .../integrationSettings.jsx | 2 +- ui/src/components/utils.js | 19 +++++++++++++++++++ 4 files changed, 24 insertions(+), 4 deletions(-) rename ui/src/components/{constans.js => constants.js} (100%) create mode 100644 ui/src/components/utils.js diff --git a/ui/src/components/constans.js b/ui/src/components/constants.js similarity index 100% rename from ui/src/components/constans.js rename to ui/src/components/constants.js diff --git a/ui/src/components/integrationFormFields/integrationFormFields.jsx b/ui/src/components/integrationFormFields/integrationFormFields.jsx index e54f4ae..eb68c5d 100644 --- a/ui/src/components/integrationFormFields/integrationFormFields.jsx +++ b/ui/src/components/integrationFormFields/integrationFormFields.jsx @@ -1,11 +1,12 @@ import React, { useEffect } from 'react'; -import { LABELS } from '../constans'; +import { LABELS } from '../constants'; +import { btsJiraCloudUrl } from '../utils'; export const IntegrationFormFields = (props) => { const { initialize, disabled, initialData, updateMetaData, ...extensionProps } = props; const { components: { FieldErrorHint, FieldElement, FieldText, FieldTextFlex }, - validators: { requiredField, btsUrl, btsProjectKey, btsIntegrationName, email }, + validators: { requiredField, btsProjectKey, btsIntegrationName, email }, constants: { SECRET_FIELDS_KEY }, } = extensionProps; @@ -33,7 +34,7 @@ export const IntegrationFormFields = (props) => { { const { data, goToPreviousPage, onUpdate, isGlobal, ...extensionProps } = props; diff --git a/ui/src/components/utils.js b/ui/src/components/utils.js new file mode 100644 index 0000000..37fecbc --- /dev/null +++ b/ui/src/components/utils.js @@ -0,0 +1,19 @@ +const regex = (regexStr) => (value) => RegExp(regexStr).test(value); +const trimValue = (value) => (typeof value === 'string' ? value.trim() : value); +const isEmpty = (value) => { + const trimmedValue = trimValue(value); + return trimmedValue === '' || trimmedValue === undefined || trimmedValue === null; +}; +const isNotEmpty = (value) => !isEmpty(value); +const composeValidators = (validators) => (value) => + validators.every((validator) => validator(value)); + +const jiraCloudUrl = composeValidators([ + isNotEmpty, + regex(/https:\/\/[^?]*.atlassian.(net|com)\/.*/), +]); + +const bindMessageToValidator = (validator, errorMessage) => (value) => + !validator(value) ? errorMessage : undefined; + +export const btsJiraCloudUrl = bindMessageToValidator(jiraCloudUrl, 'btsUrlHint');