Skip to content

Commit 811b41b

Browse files
Merge pull request #4005 from Harshit-Soni05/Validation-for-Hub-Names
Validation for Hub Names SPRW-691
2 parents 153abf9 + da48b59 commit 811b41b

File tree

7 files changed

+115
-14
lines changed

7 files changed

+115
-14
lines changed

packages/@sparrow-common/src/features/create-team/components/team-name/TeamName.svelte

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424
*/
2525
const inputId: string = "team-name-input";
2626
27-
const isOnlySpecialCharacters = (teamName: string) => {
28-
// This regex checks if the string contains ONLY non-alphanumeric characters
29-
return !/^(?!.*[^A-Za-z0-9]{3,})(?=.*[A-Za-z0-9])[\x20-\x7E]+$/.test(
30-
teamName,
31-
);
27+
const isValidHubName = (name: string) => {
28+
const trimmedName = name.trim();
29+
// Check if empty
30+
if (!trimmedName) return false;
31+
// Check if contains at least one alphanumeric character
32+
const hasAlphanumeric = /[a-zA-Z0-9]/.test(trimmedName);
33+
// Check if contains only allowed characters (letters, digits, spaces, ., -, _)
34+
const onlyAllowedChars = /^[a-zA-Z0-9._\- ]+$/.test(trimmedName);
35+
return hasAlphanumeric && onlyAllowedChars;
3236
};
3337
</script>
3438

@@ -53,11 +57,8 @@
5357
bind:value={teamForm.name.value}
5458
on:blur={() => {
5559
teamForm.name.isTouched = true;
56-
teamForm.name.value = teamForm.name.value.trim(); // Trim the value on blur
57-
if (
58-
teamForm.name.value &&
59-
isOnlySpecialCharacters(teamForm.name.value)
60-
) {
60+
teamForm.name.value = teamForm.name.value.trim();
61+
if (teamForm.name.value && !isValidHubName(teamForm.name.value)) {
6162
teamForm.name.invalid = true;
6263
} else {
6364
teamForm.name.invalid = false;

packages/@sparrow-common/src/features/create-team/constants/name.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ export const NAME_CONFIG = {
33
MAX_TEXT_SIZE: 100,
44
REQUIRED_ERROR_MESSAGE: `Please enter your hub name.`,
55
PLACEHOLDER: `Enter your hub name`,
6-
INVALID_ERROR_MESSAGE: `Invalid team name. Please remove unsupported characters like emojis or more than two special symbols.`,
6+
INVALID_ERROR_MESSAGE: `Hub names can contain combination of letters, digits and these special characters (.,-,_). Please provide the hub names accordingly.`,
77
};

packages/@sparrow-teams/src/features/create-workspace/components/workspace-name/WorkspaceName.svelte

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@
2323
* Data
2424
*/
2525
const inputId: string = "team-name-input";
26+
27+
const isValidWorkspaceName = (name: string) => {
28+
const trimmedName = name.trim();
29+
// Check if empty
30+
if (!trimmedName) return false;
31+
// Check if contains at least one alphanumeric character
32+
const hasAlphanumeric = /[a-zA-Z0-9]/.test(trimmedName);
33+
// Check if contains only allowed characters (letters, digits, spaces, ., -, _)
34+
const onlyAllowedChars = /^[a-zA-Z0-9._\- ]+$/.test(trimmedName);
35+
return hasAlphanumeric && onlyAllowedChars;
36+
};
2637
</script>
2738

2839
<div class="pb-4 mt-3">
@@ -34,20 +45,38 @@
3445
inputValueRequired={true}
3546
headerLabelText={NAME_CONFIG.TITLE}
3647
helpLabel={true}
37-
isError={!workspaceForm.name.value.trim() && workspaceForm.name.isTouched}
38-
errorMessage={NAME_CONFIG.REQUIRED_ERROR_MESSAGE}
48+
isError={(!workspaceForm.name.value.trim() &&
49+
workspaceForm.name.isTouched) ||
50+
workspaceForm.name.invalid}
51+
errorMessage={!workspaceForm.name.value.trim() &&
52+
workspaceForm.name.isTouched
53+
? NAME_CONFIG.REQUIRED_ERROR_MESSAGE
54+
: workspaceForm.name.invalid
55+
? NAME_CONFIG.INVALID_ERROR_MESSAGE
56+
: ""}
3957
>
4058
<Input
4159
bind:value={workspaceForm.name.value}
4260
on:blur={() => {
4361
workspaceForm.name.isTouched = true;
62+
workspaceForm.name.value = workspaceForm.name.value.trim();
63+
if (
64+
workspaceForm.name.value &&
65+
!isValidWorkspaceName(workspaceForm.name.value)
66+
) {
67+
workspaceForm.name.invalid = true;
68+
} else {
69+
workspaceForm.name.invalid = false;
70+
}
4471
}}
4572
height={"36px"}
4673
id={inputId}
4774
placeholder={NAME_CONFIG.PLACEHOLDER}
4875
class="text-fs-14 bg-tertiary-300 fw-normal px-2 border-radius-4"
4976
style="outline:none;"
50-
isError={!workspaceForm.name.value.trim() && workspaceForm.name.isTouched}
77+
isError={(!workspaceForm.name.value.trim() &&
78+
workspaceForm.name.isTouched) ||
79+
workspaceForm.name.invalid}
5180
isEditIconRequired={false}
5281
type={"text"}
5382
placeholderColor={"var(--text-secondary-200)"}

packages/@sparrow-teams/src/features/create-workspace/constants/name.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export const NAME_CONFIG = {
22
TITLE: `Workspace Name`,
33
REQUIRED_ERROR_MESSAGE: `Workspace name cannot be empty.`,
44
PLACEHOLDER: `Enter workspace name`,
5+
INVALID_ERROR_MESSAGE: `Workspace names can contain combination of letters, digits and these special characters (.,-,_). Please provide the workspace names accordingly.`,
56
};

packages/@sparrow-workspaces/src/features/collection-list/components/collection/Collection.svelte

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { HttpRequestDefaultNameBaseEnum } from "@sparrow/common/types/workspace/http-request-base";
55
import { slide } from "svelte/transition";
66
import { captureEvent } from "@app/utils/posthog/posthogConfig";
7+
import { notifications } from "@sparrow/library/ui";
78
export let onItemCreated: (entityType: string, args: any) => void;
89
export let onItemDeleted: (entityType: string, args: any) => void;
910
export let onItemRenamed: (entityType: string, args: any) => void;
@@ -484,8 +485,27 @@
484485
newCollectionName = target.value.trim();
485486
};
486487
488+
const isValidCollectionName = (name: string) => {
489+
const trimmedName = name.trim();
490+
// Check if empty
491+
if (!trimmedName) return false;
492+
// Check if contains at least one alphanumeric character
493+
const hasAlphanumeric = /[a-zA-Z0-9]/.test(trimmedName);
494+
// Check if contains only allowed characters (letters, digits, spaces, ., -, _)
495+
const onlyAllowedChars = /^[a-zA-Z0-9._\- ]+$/.test(trimmedName);
496+
return hasAlphanumeric && onlyAllowedChars;
497+
};
498+
487499
const onRenameBlur = async () => {
488500
if (newCollectionName) {
501+
if (!isValidCollectionName(newCollectionName)) {
502+
notifications.error(
503+
"Collection names can contain combination of letters, digits and these special characters (.,-,_). Please provide the collection names accordingly.",
504+
);
505+
isRenaming = false;
506+
newCollectionName = "";
507+
return;
508+
}
489509
await onItemRenamed("collection", {
490510
workspaceId: collection.workspaceId,
491511
collection,

packages/@sparrow-workspaces/src/features/collection-list/components/folder/Folder.svelte

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
Button,
2626
Options,
2727
Tooltip,
28+
notifications,
2829
} from "@sparrow/library/ui";
2930
3031
// ---- Enum, Constants and Interface
@@ -387,8 +388,27 @@
387388
newFolderName = target.value.trim();
388389
};
389390
391+
const isValidFolderName = (name: string) => {
392+
const trimmedName = name.trim();
393+
// Check if empty
394+
if (!trimmedName) return false;
395+
// Check if contains at least one alphanumeric character
396+
const hasAlphanumeric = /[a-zA-Z0-9]/.test(trimmedName);
397+
// Check if contains only allowed characters (letters, digits, spaces, ., -, _)
398+
const onlyAllowedChars = /^[a-zA-Z0-9._\- ]+$/.test(trimmedName);
399+
return hasAlphanumeric && onlyAllowedChars;
400+
};
401+
390402
const onRenameBlur = async () => {
391403
if (newFolderName) {
404+
if (!isValidFolderName(newFolderName)) {
405+
notifications.error(
406+
"Folder names can contain combination of letters, digits and these special characters (.,-,_). Please provide the folder names accordingly.",
407+
);
408+
isRenaming = false;
409+
newFolderName = "";
410+
return;
411+
}
392412
await onItemRenamed("folder", {
393413
workspaceId: collection.workspaceId,
394414
collection,

packages/@sparrow-workspaces/src/features/save-as-request/layout/SaveAsRequest.svelte

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,21 @@
184184
};
185185
186186
const handleCreateFolder = async (folderName: string): Promise<void> => {
187+
const trimmedName = folderName.trim();
188+
// Validate folder name
189+
if (!trimmedName) {
190+
notifications.error("Folder name cannot be empty.");
191+
return;
192+
}
193+
const hasAlphanumeric = /[a-zA-Z0-9]/.test(trimmedName);
194+
const onlyAllowedChars = /^[a-zA-Z0-9._\- ]+$/.test(trimmedName);
195+
if (!hasAlphanumeric || !onlyAllowedChars) {
196+
notifications.error(
197+
"Folder names can contain combination of letters, digits and these special characters (.,-,_). Please provide the folder names accordingly.",
198+
);
199+
return;
200+
}
201+
187202
createDirectoryLoader = true;
188203
const res = await onCreateFolder(workspaceMeta, path[0].id, folderName);
189204
if (res.status === "success") {
@@ -201,6 +216,21 @@
201216
};
202217
203218
const handleCreateCollection = async (collectionName: string) => {
219+
const trimmedName = collectionName.trim();
220+
// Validate collection name
221+
if (!trimmedName) {
222+
notifications.error("Collection name cannot be empty.");
223+
return;
224+
}
225+
const hasAlphanumeric = /[a-zA-Z0-9]/.test(trimmedName);
226+
const onlyAllowedChars = /^[a-zA-Z0-9._\- ]+$/.test(trimmedName);
227+
if (!hasAlphanumeric || !onlyAllowedChars) {
228+
notifications.error(
229+
"Collection names can contain combination of letters, digits and these special characters (.,-,_). Please provide the collection names accordingly.",
230+
);
231+
return;
232+
}
233+
204234
createDirectoryLoader = true;
205235
const res = await onCreateCollection(workspaceMeta, collectionName);
206236
if (res.status === "success") {

0 commit comments

Comments
 (0)