Skip to content

Commit de85a10

Browse files
committed
fix: standardize check for username length (3-32 characters)
1 parent d5e4233 commit de85a10

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { deepFreeze } from '../common/deepFreeze';
2+
3+
export const UserConst = deepFreeze({
4+
USERNAME_MIN_LENGTH: 3,
5+
USERNAME_MAX_LENGTH: 32,
6+
});

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { ApiProperty } from '@nestjs/swagger';
22
import { IsString, IsNotEmpty, MinLength, MaxLength } from 'class-validator';
3+
import { UserConst } from '../constants';
34

45
export class UpdateUsernameDto {
56
@IsString()
6-
@MaxLength(64)
7-
@MinLength(3)
7+
@MaxLength(UserConst.USERNAME_MAX_LENGTH)
8+
@MinLength(UserConst.USERNAME_MIN_LENGTH)
89
@ApiProperty({
910
description: 'Username of the user',
1011
example: 'tomast1137',

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { SubmitHandler, useForm } from 'react-hook-form';
2323
import ClientAxios from '@web/src/lib/axios/ClientAxios';
2424
import { AxiosError } from 'axios';
2525
import toast from 'react-hot-toast';
26+
import { UserConst } from '@shared/validation/user/constants';
2627

2728
interface FormValues {
2829
username: string;
@@ -141,10 +142,18 @@ export const UserMenu = ({ userData }: { userData: LoggedUserData }) => {
141142
{...register('username', {
142143
required: 'Username is required',
143144
pattern: {
144-
value: /^[a-zA-Z0-9-_.]{1,32}$/,
145+
value: /^[a-zA-Z0-9-_.]*$/,
145146
message:
146147
'Your username may only contain these characters: A-Z a-z 0-9 - _ .',
147148
},
149+
maxLength: {
150+
value: UserConst.USERNAME_MAX_LENGTH,
151+
message: `The username must be shorter than ${UserConst.USERNAME_MAX_LENGTH} characters`,
152+
},
153+
minLength: {
154+
value: UserConst.USERNAME_MIN_LENGTH,
155+
message: `The username must have at least ${UserConst.USERNAME_MIN_LENGTH} characters`,
156+
},
148157
})}
149158
/>
150159
<button

0 commit comments

Comments
 (0)