File size: 2,820 Bytes
3120563
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
class ValidationMiddleware {
    static validateEmail(email) {
        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        return emailRegex.test(email);
    }

    static validateUsername(username) {
        const usernameRegex = /^[a-zA-Z0-9_]{3,20}$/;
        return usernameRegex.test(username);
    }

    static validatePassword(password) {
        return password.length >= 6;
    }

    static validateRegisterData(data) {
        const errors = [];

        if (!data.username) {
            errors.push('Username is required');
        } else if (!this.validateUsername(data.username)) {
            errors.push('Username must be 3-20 characters and contain only letters, numbers, and underscores');
        }

        if (!data.email) {
            errors.push('Email is required');
        } else if (!this.validateEmail(data.email)) {
            errors.push('Invalid email format');
        }

        if (!data.password) {
            errors.push('Password is required');
        } else if (!this.validatePassword(data.password)) {
            errors.push('Password must be at least 6 characters');
        }

        if (data.password !== data.confirmPassword) {
            errors.push('Passwords do not match');
        }

        return {
            isValid: errors.length === 0,
            errors: errors
        };
    }

    static validateLoginData(data) {
        const errors = [];

        if (!data.username) {
            errors.push('Username is required');
        }

        if (!data.password) {
            errors.push('Password is required');
        }

        return {
            isValid: errors.length === 0,
            errors: errors
        };
    }

    static validateProfileData(data) {
        const errors = [];

        if (data.email && !this.validateEmail(data.email)) {
            errors.push('Invalid email format');
        }

        if (data.fullName && data.fullName.length < 2) {
            errors.push('Full name must be at least 2 characters');
        }

        return {
            isValid: errors.length === 0,
            errors: errors
        };
    }

    static sanitizeInput(input) {
        if (typeof input !== 'string') return input;
        
        return input
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#x27;')
            .replace(/\//g, '&#x2F;');
    }

    static validateExerciseAnswer(answer) {
        const errors = [];

        if (!answer || answer.trim().length === 0) {
            errors.push('Answer cannot be empty');
        }

        if (answer.length > 10000) {
            errors.push('Answer is too long');
        }

        return {
            isValid: errors.length === 0,
            errors: errors
        };
    }
}