Skip to content

Commit 02376ca

Browse files
committed
refactor(utm): define maximum UTM value length as a constant for better maintainability
1 parent bd6d64a commit 02376ca

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/utils/utm/utm.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ const VALID_UTM_CHARACTERS = /^[a-zA-Z0-9\s\-_.]+$/;
1717
*/
1818
const INVALID_UTM_CHARACTERS = /[^a-zA-Z0-9\s\-_.]/g;
1919

20+
/**
21+
* Maximum allowed length for UTM parameter values
22+
*/
23+
const MAX_UTM_VALUE_LENGTH = 200;
24+
2025
/**
2126
* Validates UTM parameters
2227
* @param utm - Data form where user went to sign up. Used for analytics purposes
@@ -53,7 +58,7 @@ export function validateUtmParams(utm: UserDBScheme['utm']): boolean {
5358
}
5459

5560
// Check length
56-
if (value.length === 0 || value.length > 200) {
61+
if (value.length === 0 || value.length > MAX_UTM_VALUE_LENGTH) {
5762
return false;
5863
}
5964

@@ -82,7 +87,10 @@ export function sanitizeUtmParams(utm: UserDBScheme['utm']): UserDBScheme['utm']
8287
for (const [key, value] of Object.entries(utm)) {
8388
if (VALID_UTM_KEYS.includes(key) && value && typeof value === 'string') {
8489
// Sanitize value: keep only allowed characters and limit length
85-
const cleanValue = value.replace(INVALID_UTM_CHARACTERS, '').trim().substring(0, 200);
90+
const cleanValue = value
91+
.replace(INVALID_UTM_CHARACTERS, '')
92+
.trim()
93+
.substring(0, MAX_UTM_VALUE_LENGTH);
8694

8795
if (cleanValue.length > 0) {
8896
(sanitized as any)[key] = cleanValue;

0 commit comments

Comments
 (0)