Skip to content

Commit 71c15bd

Browse files
committed
feat: add UpdateUserProfile DTO and validation for social media links
1 parent 370757f commit 71c15bd

File tree

2 files changed

+220
-0
lines changed

2 files changed

+220
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { validate } from 'class-validator';
2+
import { UserLinks } from './UpdateUserProfile.dto';
3+
4+
describe('UpdateUserProfileDto', () => {
5+
describe('UserLinks', () => {
6+
it('should validate valid URLs', async () => {
7+
const userLinks = new UserLinks();
8+
9+
userLinks.github = 'https://github.com/tomast1337';
10+
userLinks.youtube = 'https://www.youtube.com/@Bentroen_';
11+
userLinks.spotify =
12+
'https://open.spotify.com/artist/1McMsnEElThX1knmY4oliG?si=v95i3XbRRgKT9JwyiFiFEg';
13+
userLinks.bandcamp = 'https://igorrr.bandcamp.com/';
14+
userLinks.facebook = 'https://www.facebook.com/MrBean';
15+
userLinks.reddit = 'https://www.reddit.com/user/Unidan/';
16+
userLinks.soundcloud = 'https://soundcloud.com/futureisnow';
17+
userLinks.steam = 'https://steamcommunity.com/id/CattleDecapitation/';
18+
userLinks.x = 'https://x.com/Trail_Cams';
19+
userLinks.twitch = 'https://www.twitch.tv/vinesauce';
20+
userLinks.threads = 'https://www.threads.net/@kimkardashian';
21+
userLinks.tiktok = 'https://www.tiktok.com/@karolg';
22+
userLinks.snapchat = 'https://www.snapchat.com/add/username';
23+
userLinks.instagram = 'https://instagram.com/validuser';
24+
userLinks.discord = 'https://discord.com/validuser';
25+
userLinks.telegram = 'https://t.me/validuser';
26+
27+
const errors = await validate(userLinks);
28+
console.log(errors);
29+
expect(errors.length).toBe(0);
30+
});
31+
32+
it('should invalidate invalid URLs', async () => {
33+
const userLinks = new UserLinks();
34+
userLinks.bandcamp = 'invalid-url';
35+
userLinks.discord = 'invalid-url';
36+
userLinks.facebook = 'invalid-url';
37+
userLinks.github = 'invalid-url';
38+
userLinks.instagram = 'invalid-url';
39+
userLinks.reddit = 'invalid-url';
40+
userLinks.snapchat = 'invalid-url';
41+
userLinks.soundcloud = 'invalid-url';
42+
userLinks.spotify = 'invalid-url';
43+
userLinks.steam = 'invalid-url';
44+
userLinks.telegram = 'invalid-url';
45+
userLinks.tiktok = 'invalid-url';
46+
userLinks.threads = 'invalid-url';
47+
userLinks.twitch = 'invalid-url';
48+
userLinks.x = 'invalid-url';
49+
userLinks.youtube = 'invalid-url';
50+
51+
const errors = await validate(userLinks);
52+
expect(errors.length).toBeGreaterThan(0);
53+
});
54+
55+
it('should allow optional fields to be empty', async () => {
56+
const userLinks = new UserLinks();
57+
58+
const errors = await validate(userLinks);
59+
expect(errors.length).toBe(0);
60+
});
61+
});
62+
});
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { Type } from 'class-transformer';
3+
import {
4+
IsOptional,
5+
IsString,
6+
IsUrl,
7+
Matches,
8+
MaxLength,
9+
MinLength,
10+
} from 'class-validator';
11+
12+
export const LinkRegexes = {
13+
bandcamp: /https?:\/\/[a-zA-Z0-9_-]+\.bandcamp\.com\/?/,
14+
discord: /https?:\/\/(www\.)?discord\.com\/[a-zA-Z0-9_]+/,
15+
facebook: /https?:\/\/(www\.)?facebook\.com\/[a-zA-Z0-9_]+/,
16+
github: /https?:\/\/(www\.)?github\.com\/[a-zA-Z0-9_-]+/,
17+
instagram: /https?:\/\/(www\.)?instagram\.com\/[a-zA-Z0-9_]+/,
18+
reddit: /https?:\/\/(www\.)?reddit\.com\/user\/[a-zA-Z0-9_-]+/,
19+
snapchat: /https?:\/\/(www\.)?snapchat\.com\/add\/[a-zA-Z0-9_-]+/,
20+
soundcloud: /https?:\/\/(www\.)?soundcloud\.com\/[a-zA-Z0-9_-]+/,
21+
spotify: /https?:\/\/open\.spotify\.com\/artist\/[a-zA-Z0-9?&=]+/,
22+
steam: /https?:\/\/steamcommunity\.com\/id\/[a-zA-Z0-9_-]+/,
23+
telegram: /https?:\/\/(www\.)?t\.me\/[a-zA-Z0-9_]+/,
24+
tiktok: /https?:\/\/(www\.)?tiktok\.com\/@?[a-zA-Z0-9_]+/,
25+
threads: /https?:\/\/(www\.)?threads\.net\/@?[a-zA-Z0-9_]+/,
26+
twitch: /https?:\/\/(www\.)?twitch\.tv\/[a-zA-Z0-9_]+/,
27+
x: /https?:\/\/(www\.)?x\.com\/[a-zA-Z0-9_]+/,
28+
youtube: /https?:\/\/(www\.)?youtube\.com\/@?[a-zA-Z0-9_-]+/,
29+
};
30+
31+
export class UserLinks {
32+
@IsOptional()
33+
@IsUrl()
34+
@Matches(LinkRegexes.bandcamp)
35+
bandcamp?: string;
36+
37+
@IsOptional()
38+
@IsUrl()
39+
@Matches(LinkRegexes.discord)
40+
discord?: string;
41+
42+
@IsOptional()
43+
@IsUrl()
44+
@Matches(LinkRegexes.facebook)
45+
facebook?: string;
46+
47+
@IsOptional()
48+
@IsUrl()
49+
@Matches(LinkRegexes.github)
50+
github?: string;
51+
52+
@IsOptional()
53+
@IsUrl()
54+
@Matches(LinkRegexes.instagram)
55+
instagram?: string;
56+
57+
@IsOptional()
58+
@IsUrl()
59+
@Matches(LinkRegexes.reddit)
60+
reddit?: string;
61+
62+
@IsOptional()
63+
@IsUrl()
64+
@Matches(LinkRegexes.snapchat)
65+
snapchat?: string;
66+
67+
@IsOptional()
68+
@IsUrl()
69+
@Matches(LinkRegexes.soundcloud)
70+
soundcloud?: string;
71+
72+
@IsOptional()
73+
@IsUrl()
74+
@Matches(LinkRegexes.spotify)
75+
spotify?: string;
76+
77+
@IsOptional()
78+
@IsUrl()
79+
@Matches(LinkRegexes.steam)
80+
steam?: string;
81+
82+
@IsOptional()
83+
@IsUrl()
84+
@Matches(LinkRegexes.telegram)
85+
telegram?: string;
86+
87+
@IsOptional()
88+
@IsUrl()
89+
@Matches(LinkRegexes.tiktok)
90+
tiktok?: string;
91+
92+
@IsOptional()
93+
@IsUrl()
94+
@Matches(LinkRegexes.threads)
95+
threads?: string;
96+
97+
@IsOptional()
98+
@IsUrl()
99+
@Matches(LinkRegexes.twitch)
100+
twitch?: string;
101+
102+
@IsOptional()
103+
@IsUrl()
104+
@Matches(LinkRegexes.x)
105+
x?: string;
106+
107+
@IsOptional()
108+
@IsUrl()
109+
@Matches(LinkRegexes.youtube)
110+
youtube?: string;
111+
}
112+
113+
export class UpdateUserProfileDto {
114+
@IsString()
115+
@MaxLength(64)
116+
@MinLength(3)
117+
@IsOptional()
118+
@ApiProperty({
119+
description: 'Username of the user',
120+
example: 'tomast1137',
121+
})
122+
username?: string;
123+
124+
@IsOptional()
125+
@IsString()
126+
@MaxLength(1024)
127+
@ApiProperty({
128+
description: 'Description of the user',
129+
example: 'I using noteblock.world',
130+
})
131+
description?: string;
132+
133+
@IsOptional()
134+
@Type(() => UserLinks)
135+
@ApiProperty({
136+
description: 'Social media links of the user',
137+
example: {
138+
github: 'https://github.com/tomast1337',
139+
youtube: 'https://www.youtube.com/@Bentroen_',
140+
spotify:
141+
'https://open.spotify.com/artist/1McMsnEElThX1knmY4oliG?si=v95i3XbRRgKT9JwyiFiFEg',
142+
bandcamp: 'https://igorrr.bandcamp.com/',
143+
facebook: 'https://www.facebook.com/MrBean',
144+
reddit: 'https://www.reddit.com/user/Unidan/',
145+
soundcloud: 'https://soundcloud.com/futureisnow',
146+
steam: 'https://steamcommunity.com/id/CattleDecapitation/',
147+
x: 'https://x.com/Trail_Cams',
148+
twitch: 'https://www.twitch.tv/vinesauce',
149+
threads: 'https://www.threads.net/@kimkardashian',
150+
tiktok: 'https://www.tiktok.com/@karolg',
151+
snapchat: 'https://www.snapchat.com/add/username',
152+
instagram: 'https://instagram.com/validuser',
153+
discord: 'https://discord.com/validuser',
154+
telegram: 'https://t.me/validuser',
155+
},
156+
})
157+
socialLinks?: UserLinks;
158+
}

0 commit comments

Comments
 (0)