Elffuss BraidHub

kikocis/continue public

main · 1.1 KiBRawPermalink
1// A simple user validation utility with poor organization
2// Difficulty: 1 - Easy fixes needed
3
4type UserData = {
5 id?: number;
6 name: string;
7 email: string;
8 age?: number;
9 isActive: boolean;
10};
11
12// Global variable to store error messages
13let validationError = "";
14
15// Function to validate user data
16function validateUser(data: UserData) {
17 // Reset error
18 validationError = "";
19
20 // Check name
21 if (data.name.length < 2) {
22 validationError = "Name is too short";
23 return false;
24 }
25
26 // Check email
27 if (!data.email.includes("@")) {
28 validationError = "Invalid email format";
29 return false;
30 }
31
32 // Check age if provided
33 if (data.age != undefined) {
34 if (data.age < 18) {
35 validationError = "User must be 18 or older";
36 return false;
37 }
38 }
39
40 // All validations passed
41 return true;
42}
43
44// Example usage
45const user = {
46 name: "Jo",
47 email: "jo.example.com",
48 age: 25,
49 isActive: true,
50};
51
52if (validateUser(user)) {
53 console.log("User is valid");
54} else {
55 console.log("Error: " + validationError);
56}
57
58export { validateUser, validationError };