Skip to content

Commit 213c543

Browse files
authored
chore: mock server name validation and prevent duplicates (hoppscotch#5524)
1 parent 881c715 commit 213c543

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

packages/hoppscotch-backend/src/mock-server/mock-server.model.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@ import {
77
registerEnumType,
88
} from '@nestjs/graphql';
99
import {
10-
IsAlphanumeric,
1110
IsNumber,
1211
IsOptional,
12+
IsString,
13+
Matches,
1314
Max,
1415
MaxLength,
1516
MinLength,
1617
} from 'class-validator';
1718
import { WorkspaceType } from 'src/types/WorkspaceTypes';
1819

20+
// Regex pattern for mock server name validation
21+
// Allows letters, numbers, spaces, dots, underscores, and hyphens
22+
const MOCK_SERVER_NAME_PATTERN = /^[a-zA-Z0-9 ._-]+$/;
23+
const MOCK_SERVER_NAME_ERROR_MESSAGE =
24+
'Name can only contain letters, numbers, spaces, dots, underscores, and hyphens';
25+
1926
@ObjectType()
2027
export class MockServer {
2128
@Field(() => ID, {
@@ -102,9 +109,12 @@ export class CreateMockServerInput {
102109
@Field({
103110
description: 'Name of the mock server',
104111
})
112+
@IsString()
105113
@MinLength(1)
106114
@MaxLength(255)
107-
@IsAlphanumeric()
115+
@Matches(MOCK_SERVER_NAME_PATTERN, {
116+
message: MOCK_SERVER_NAME_ERROR_MESSAGE,
117+
})
108118
name: string;
109119

110120
@Field({
@@ -149,10 +159,13 @@ export class UpdateMockServerInput {
149159
nullable: true,
150160
description: 'Name of the mock server',
151161
})
162+
@IsString()
152163
@IsOptional()
153164
@MinLength(1)
154165
@MaxLength(255)
155-
@IsAlphanumeric()
166+
@Matches(MOCK_SERVER_NAME_PATTERN, {
167+
message: MOCK_SERVER_NAME_ERROR_MESSAGE,
168+
})
156169
name?: string;
157170

158171
@Field({

0 commit comments

Comments
 (0)