From 35e01e1280990cc8ac0c588955af099432bad99a Mon Sep 17 00:00:00 2001 From: Nivedin <53208152+nivedin@users.noreply.github.com> Date: Mon, 29 Sep 2025 13:25:40 +0530 Subject: [PATCH] chore: accept single character length name and trim white spaces (#5412) Co-authored-by: mirarifhasan --- .../team-collection.service.spec.ts | 10 +++++----- .../team-collection/team-collection.service.ts | 2 +- .../team-environments.service.spec.ts | 4 ++-- .../team-environments.service.ts | 2 +- .../hoppscotch-backend/src/team/team.service.ts | 17 +++++++---------- packages/hoppscotch-backend/src/utils.ts | 2 +- packages/hoppscotch-common/locales/en.json | 2 +- .../src/components/collections/Add.vue | 6 +++--- .../src/components/collections/AddFolder.vue | 4 ++-- .../src/components/collections/AddRequest.vue | 4 ++-- .../src/components/environments/my/Details.vue | 2 +- .../components/environments/teams/Details.vue | 6 ++++++ 12 files changed, 32 insertions(+), 29 deletions(-) diff --git a/packages/hoppscotch-backend/src/team-collection/team-collection.service.spec.ts b/packages/hoppscotch-backend/src/team-collection/team-collection.service.spec.ts index 6a91c80a..cc1dbc87 100644 --- a/packages/hoppscotch-backend/src/team-collection/team-collection.service.spec.ts +++ b/packages/hoppscotch-backend/src/team-collection/team-collection.service.spec.ts @@ -661,10 +661,10 @@ describe('getCollection', () => { }); describe('createCollection', () => { - test('should throw TEAM_COLL_SHORT_TITLE when title is less than 3 characters', async () => { + test('should throw TEAM_COLL_SHORT_TITLE when title is less than 1 character', async () => { const result = await teamCollectionService.createCollection( rootTeamCollection.teamID, - 'ab', + '', JSON.stringify(rootTeamCollection.data), rootTeamCollection.id, ); @@ -781,10 +781,10 @@ describe('createCollection', () => { }); describe('renameCollection', () => { - test('should throw TEAM_COLL_SHORT_TITLE when title is less than 3 characters', async () => { + test('should throw TEAM_COLL_SHORT_TITLE when title is less than 1 character', async () => { const result = await teamCollectionService.renameCollection( rootTeamCollection.id, - 'ab', + '', ); expect(result).toEqualLeft(TEAM_COLL_SHORT_TITLE); }); @@ -1464,7 +1464,7 @@ describe('updateTeamCollection', () => { const result = await teamCollectionService.updateTeamCollection( rootTeamCollection.id, JSON.stringify(rootTeamCollection.data), - 'de', + '', ); expect(result).toEqualLeft(TEAM_COLL_SHORT_TITLE); }); diff --git a/packages/hoppscotch-backend/src/team-collection/team-collection.service.ts b/packages/hoppscotch-backend/src/team-collection/team-collection.service.ts index bb1eabbf..ac6f9147 100644 --- a/packages/hoppscotch-backend/src/team-collection/team-collection.service.ts +++ b/packages/hoppscotch-backend/src/team-collection/team-collection.service.ts @@ -56,7 +56,7 @@ export class TeamCollectionService { private readonly teamService: TeamService, ) {} - TITLE_LENGTH = 3; + TITLE_LENGTH = 1; MAX_RETRIES = 5; // Maximum number of retries for database transactions /** diff --git a/packages/hoppscotch-backend/src/team-environments/team-environments.service.spec.ts b/packages/hoppscotch-backend/src/team-environments/team-environments.service.spec.ts index cda17f4b..318b8f5d 100644 --- a/packages/hoppscotch-backend/src/team-environments/team-environments.service.spec.ts +++ b/packages/hoppscotch-backend/src/team-environments/team-environments.service.spec.ts @@ -80,7 +80,7 @@ describe('TeamEnvironmentsService', () => { test('should throw TEAM_ENVIRONMENT_SHORT_NAME if input TeamEnvironment name is invalid', async () => { const result = await teamEnvironmentsService.createTeamEnvironment( - '12', + '', teamEnvironment.teamID, JSON.stringify(teamEnvironment.variables), ); @@ -218,7 +218,7 @@ describe('TeamEnvironmentsService', () => { test('should throw TEAM_ENVIRONMENT_SHORT_NAME if input TeamEnvironment name is invalid', async () => { const result = await teamEnvironmentsService.updateTeamEnvironment( teamEnvironment.id, - '12', + '', JSON.stringify([{ key: 'value' }]), ); diff --git a/packages/hoppscotch-backend/src/team-environments/team-environments.service.ts b/packages/hoppscotch-backend/src/team-environments/team-environments.service.ts index a7888b3d..bb85f7b6 100644 --- a/packages/hoppscotch-backend/src/team-environments/team-environments.service.ts +++ b/packages/hoppscotch-backend/src/team-environments/team-environments.service.ts @@ -19,7 +19,7 @@ export class TeamEnvironmentsService { private readonly teamService: TeamService, ) {} - TITLE_LENGTH = 3; + TITLE_LENGTH = 1; /** * TeamEnvironments are saved in the DB in the following way diff --git a/packages/hoppscotch-backend/src/team/team.service.ts b/packages/hoppscotch-backend/src/team/team.service.ts index 11ea9613..1c08b8a0 100644 --- a/packages/hoppscotch-backend/src/team/team.service.ts +++ b/packages/hoppscotch-backend/src/team/team.service.ts @@ -21,7 +21,7 @@ import * as O from 'fp-ts/Option'; import * as E from 'fp-ts/Either'; import * as T from 'fp-ts/Task'; import * as A from 'fp-ts/Array'; -import { throwErr } from 'src/utils'; +import { isValidLength, throwErr } from 'src/utils'; import { AuthUser } from '../types/AuthUser'; @Injectable() @@ -32,6 +32,8 @@ export class TeamService implements UserDataHandler, OnModuleInit { private readonly pubsub: PubSubService, ) {} + TITLE_LENGTH = 1; + onModuleInit() { this.userService.registerUserDataHandler(this); } @@ -123,17 +125,12 @@ export class TeamService implements UserDataHandler, OnModuleInit { return E.right(true); } - validateTeamName(title: string): E.Left | E.Right { - if (!title || title.trim() === '') return E.left(TEAM_NAME_INVALID); - return E.right(true); - } - async renameTeam( teamID: string, newName: string, ): Promise | E.Right> { - const isValidTitle = this.validateTeamName(newName); - if (E.isLeft(isValidTitle)) return isValidTitle; + const isValidTitle = isValidLength(newName, this.TITLE_LENGTH); + if (!isValidTitle) return E.left(TEAM_NAME_INVALID); try { const updatedTeam = await this.prisma.team.update({ @@ -245,8 +242,8 @@ export class TeamService implements UserDataHandler, OnModuleInit { name: string, creatorUid: string, ): Promise | E.Right> { - const isValidName = this.validateTeamName(name); - if (E.isLeft(isValidName)) return isValidName; + const isValidName = isValidLength(name, this.TITLE_LENGTH); + if (!isValidName) return E.left(TEAM_NAME_INVALID); const team = await this.prisma.team.create({ data: { diff --git a/packages/hoppscotch-backend/src/utils.ts b/packages/hoppscotch-backend/src/utils.ts index a8e618f2..4ac5423f 100644 --- a/packages/hoppscotch-backend/src/utils.ts +++ b/packages/hoppscotch-backend/src/utils.ts @@ -229,7 +229,7 @@ export function stringToJson( * @returns boolean if title is of valid length or not */ export function isValidLength(title: string, length: number) { - if (title.length < length) { + if (!title || title.trim() === '' || title.length < length) { return false; } diff --git a/packages/hoppscotch-common/locales/en.json b/packages/hoppscotch-common/locales/en.json index 07b36df8..a2e2323b 100644 --- a/packages/hoppscotch-common/locales/en.json +++ b/packages/hoppscotch-common/locales/en.json @@ -477,7 +477,7 @@ "select": "Select environment", "set": "Set environment", "set_as_environment": "Set as environment", - "short_name": "Environment needs to have minimum 3 characters", + "short_name": "Environment needs to have minimum 1 character in its name", "team_environments": "Workspace Environments", "title": "Environments", "updated": "Environment updated", diff --git a/packages/hoppscotch-common/src/components/collections/Add.vue b/packages/hoppscotch-common/src/components/collections/Add.vue index 4d2a81d4..e2708fff 100644 --- a/packages/hoppscotch-common/src/components/collections/Add.vue +++ b/packages/hoppscotch-common/src/components/collections/Add.vue @@ -43,8 +43,8 @@ const t = useI18n() const props = withDefaults( defineProps<{ - show: boolean - loadingState: boolean + show?: boolean + loadingState?: boolean }>(), { show: false, @@ -73,7 +73,7 @@ const addNewCollection = () => { return } - if (!editingName.value) { + if (editingName.value.trim() === "") { toast.error(t("collection.invalid_name")) return } diff --git a/packages/hoppscotch-common/src/components/collections/AddFolder.vue b/packages/hoppscotch-common/src/components/collections/AddFolder.vue index 30cec3b0..faad5dad 100644 --- a/packages/hoppscotch-common/src/components/collections/AddFolder.vue +++ b/packages/hoppscotch-common/src/components/collections/AddFolder.vue @@ -43,8 +43,8 @@ const t = useI18n() const props = withDefaults( defineProps<{ - show: boolean - loadingState: boolean + show?: boolean + loadingState?: boolean }>(), { show: false, diff --git a/packages/hoppscotch-common/src/components/collections/AddRequest.vue b/packages/hoppscotch-common/src/components/collections/AddRequest.vue index 358ff062..381e7696 100644 --- a/packages/hoppscotch-common/src/components/collections/AddRequest.vue +++ b/packages/hoppscotch-common/src/components/collections/AddRequest.vue @@ -48,8 +48,8 @@ const t = useI18n() const props = withDefaults( defineProps<{ - show: boolean - loadingState: boolean + show?: boolean + loadingState?: boolean }>(), { show: false, diff --git a/packages/hoppscotch-common/src/components/environments/my/Details.vue b/packages/hoppscotch-common/src/components/environments/my/Details.vue index ba773fec..6ceb12bd 100644 --- a/packages/hoppscotch-common/src/components/environments/my/Details.vue +++ b/packages/hoppscotch-common/src/components/environments/my/Details.vue @@ -512,7 +512,7 @@ const saveEnvironment = () => { return } - if (editingName.value.length < 3) { + if (editingName.value.trim().length === 0) { toast.error(`${t("environment.short_name")}`) return } diff --git a/packages/hoppscotch-common/src/components/environments/teams/Details.vue b/packages/hoppscotch-common/src/components/environments/teams/Details.vue index d8f3a5cc..ba40ed11 100644 --- a/packages/hoppscotch-common/src/components/environments/teams/Details.vue +++ b/packages/hoppscotch-common/src/components/environments/teams/Details.vue @@ -496,6 +496,12 @@ const saveEnvironment = async () => { return } + if (editingName.value.trim().length === 0) { + isLoading.value = false + toast.error(`${t("environment.short_name")}`) + return + } + const filteredVariables = pipe( vars.value, A.filterMap(