Skip to content

Commit 383375c

Browse files
committed
fix: standardize regex validation pattern for username change
1 parent de85a10 commit 383375c

File tree

3 files changed

+4
-2
lines changed

3 files changed

+4
-2
lines changed

shared/validation/user/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import { deepFreeze } from '../common/deepFreeze';
33
export const UserConst = deepFreeze({
44
USERNAME_MIN_LENGTH: 3,
55
USERNAME_MAX_LENGTH: 32,
6+
ALLOWED_REGEXP: /^[a-zA-Z0-9-_.]*$/,
67
});

shared/validation/user/dto/UpdateUsername.dto.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { ApiProperty } from '@nestjs/swagger';
2-
import { IsString, IsNotEmpty, MinLength, MaxLength } from 'class-validator';
2+
import { IsString, MinLength, MaxLength, Matches } from 'class-validator';
33
import { UserConst } from '../constants';
44

55
export class UpdateUsernameDto {
66
@IsString()
77
@MaxLength(UserConst.USERNAME_MAX_LENGTH)
88
@MinLength(UserConst.USERNAME_MIN_LENGTH)
9+
@Matches(UserConst.ALLOWED_REGEXP)
910
@ApiProperty({
1011
description: 'Username of the user',
1112
example: 'tomast1137',

web/src/modules/shared/components/layout/UserMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export const UserMenu = ({ userData }: { userData: LoggedUserData }) => {
142142
{...register('username', {
143143
required: 'Username is required',
144144
pattern: {
145-
value: /^[a-zA-Z0-9-_.]*$/,
145+
value: UserConst.ALLOWED_REGEXP,
146146
message:
147147
'Your username may only contain these characters: A-Z a-z 0-9 - _ .',
148148
},

0 commit comments

Comments
 (0)