Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

chore: mock server name validation and prevent duplicates (#5524)

Mir Arif Hasan213c5436bc16
diff --git a/packages/hoppscotch-backend/src/mock-server/mock-server.model.ts b/packages/hoppscotch-backend/src/mock-server/mock-server.model.ts
index fc10743e3..cd491518f 100644
--- a/packages/hoppscotch-backend/src/mock-server/mock-server.model.ts
+++ b/packages/hoppscotch-backend/src/mock-server/mock-server.model.ts
@@ -7,15 +7,22 @@ import {
registerEnumType,
} from '@nestjs/graphql';
import {
- IsAlphanumeric,
IsNumber,
IsOptional,
+ IsString,
+ Matches,
Max,
MaxLength,
MinLength,
} from 'class-validator';
import { WorkspaceType } from 'src/types/WorkspaceTypes';
+// Regex pattern for mock server name validation
+// Allows letters, numbers, spaces, dots, underscores, and hyphens
+const MOCK_SERVER_NAME_PATTERN = /^[a-zA-Z0-9 ._-]+$/;
+const MOCK_SERVER_NAME_ERROR_MESSAGE =
+ 'Name can only contain letters, numbers, spaces, dots, underscores, and hyphens';
+
@ObjectType()
export class MockServer {
@Field(() => ID, {
@@ -102,9 +109,12 @@ export class CreateMockServerInput {
@Field({
description: 'Name of the mock server',
})
+ @IsString()
@MinLength(1)
@MaxLength(255)
- @IsAlphanumeric()
+ @Matches(MOCK_SERVER_NAME_PATTERN, {
+ message: MOCK_SERVER_NAME_ERROR_MESSAGE,
+ })
name: string;
@Field({
@@ -149,10 +159,13 @@ export class UpdateMockServerInput {
nullable: true,
description: 'Name of the mock server',
})
+ @IsString()
@IsOptional()
@MinLength(1)
@MaxLength(255)
- @IsAlphanumeric()
+ @Matches(MOCK_SERVER_NAME_PATTERN, {
+ message: MOCK_SERVER_NAME_ERROR_MESSAGE,
+ })
name?: string;
@Field({