Mhdeusi commited on
Commit
3120563
·
verified ·
1 Parent(s): 8050632

Create middleware/validation_middleware.js

Browse files
Files changed (1) hide show
  1. middleware/validation_middleware.js +108 -0
middleware/validation_middleware.js ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class ValidationMiddleware {
2
+ static validateEmail(email) {
3
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
4
+ return emailRegex.test(email);
5
+ }
6
+
7
+ static validateUsername(username) {
8
+ const usernameRegex = /^[a-zA-Z0-9_]{3,20}$/;
9
+ return usernameRegex.test(username);
10
+ }
11
+
12
+ static validatePassword(password) {
13
+ return password.length >= 6;
14
+ }
15
+
16
+ static validateRegisterData(data) {
17
+ const errors = [];
18
+
19
+ if (!data.username) {
20
+ errors.push('Username is required');
21
+ } else if (!this.validateUsername(data.username)) {
22
+ errors.push('Username must be 3-20 characters and contain only letters, numbers, and underscores');
23
+ }
24
+
25
+ if (!data.email) {
26
+ errors.push('Email is required');
27
+ } else if (!this.validateEmail(data.email)) {
28
+ errors.push('Invalid email format');
29
+ }
30
+
31
+ if (!data.password) {
32
+ errors.push('Password is required');
33
+ } else if (!this.validatePassword(data.password)) {
34
+ errors.push('Password must be at least 6 characters');
35
+ }
36
+
37
+ if (data.password !== data.confirmPassword) {
38
+ errors.push('Passwords do not match');
39
+ }
40
+
41
+ return {
42
+ isValid: errors.length === 0,
43
+ errors: errors
44
+ };
45
+ }
46
+
47
+ static validateLoginData(data) {
48
+ const errors = [];
49
+
50
+ if (!data.username) {
51
+ errors.push('Username is required');
52
+ }
53
+
54
+ if (!data.password) {
55
+ errors.push('Password is required');
56
+ }
57
+
58
+ return {
59
+ isValid: errors.length === 0,
60
+ errors: errors
61
+ };
62
+ }
63
+
64
+ static validateProfileData(data) {
65
+ const errors = [];
66
+
67
+ if (data.email && !this.validateEmail(data.email)) {
68
+ errors.push('Invalid email format');
69
+ }
70
+
71
+ if (data.fullName && data.fullName.length < 2) {
72
+ errors.push('Full name must be at least 2 characters');
73
+ }
74
+
75
+ return {
76
+ isValid: errors.length === 0,
77
+ errors: errors
78
+ };
79
+ }
80
+
81
+ static sanitizeInput(input) {
82
+ if (typeof input !== 'string') return input;
83
+
84
+ return input
85
+ .replace(/</g, '&lt;')
86
+ .replace(/>/g, '&gt;')
87
+ .replace(/"/g, '&quot;')
88
+ .replace(/'/g, '&#x27;')
89
+ .replace(/\//g, '&#x2F;');
90
+ }
91
+
92
+ static validateExerciseAnswer(answer) {
93
+ const errors = [];
94
+
95
+ if (!answer || answer.trim().length === 0) {
96
+ errors.push('Answer cannot be empty');
97
+ }
98
+
99
+ if (answer.length > 10000) {
100
+ errors.push('Answer is too long');
101
+ }
102
+
103
+ return {
104
+ isValid: errors.length === 0,
105
+ errors: errors
106
+ };
107
+ }
108
+ }