1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-09 00:18:00 +01:00

refactor: shared url_safe_basic constant (#5952)

Uses a new `URL_SAFE_BASIC` regex constant that checks for characters
that are commonly used in URL path sections: alphanumeric lowercase
characters, dashes and underscores.

This will allow us to re-use this constant in our server-side
validation.
This commit is contained in:
Nuno Góis 2024-01-18 12:33:33 +00:00 committed by GitHub
parent b91df61994
commit 3dd188e77c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 5 deletions

View File

@ -1,9 +1,8 @@
import { URL_SAFE_BASIC } from '@server/util/constants';
import { useIncomingWebhooks } from 'hooks/api/getters/useIncomingWebhooks/useIncomingWebhooks';
import { IIncomingWebhook } from 'interfaces/incomingWebhook';
import { useEffect, useState } from 'react';
const INCOMING_WEBHOOK_NAME_REGEX = /^[A-Za-z0-9\-_]*$/;
enum ErrorField {
NAME = 'name',
TOKEN_NAME = 'tokenName',
@ -66,8 +65,7 @@ export const useIncomingWebhooksForm = (incomingWebhook?: IIncomingWebhook) => {
({ id, name }) => id !== incomingWebhook?.id && name === value,
);
const isNameInvalid = (value: string) =>
!INCOMING_WEBHOOK_NAME_REGEX.test(value);
const isNameInvalid = (value: string) => !URL_SAFE_BASIC.test(value);
const validateName = (name: string) => {
if (isEmpty(name)) {
@ -83,7 +81,7 @@ export const useIncomingWebhooksForm = (incomingWebhook?: IIncomingWebhook) => {
if (isNameInvalid(name)) {
setError(
ErrorField.NAME,
'Name must only contain alphanumeric characters, dashes and underscores.',
'Name must only contain alphanumeric lowercase characters, dashes and underscores.',
);
return false;
}

View File

@ -3,6 +3,8 @@ export const DEFAULT_ENV = 'default';
export const ALL_PROJECTS = '*';
export const ALL_ENVS = '*';
export const URL_SAFE_BASIC = /^[a-z0-9\-_]*$/;
export const ROOT_PERMISSION_TYPE = 'root';
export const ENVIRONMENT_PERMISSION_TYPE = 'environment';
export const PROJECT_PERMISSION_TYPE = 'project';