Elffuss BraidHub

kikocis/continue public

main · 3.6 KiBRawPermalink
1// A refactored user validation utility following SOLID principles
2// Solution for next-edit-2-1.ts
3
4type UserData = {
5 id?: number;
6 name: string;
7 email: string;
8 age?: number;
9 isActive: boolean;
10};
11
12// Define a validation result interface
13interface ValidationResult {
14 isValid: boolean;
15 error?: string;
16}
17
18// Create a validator interface (Interface Segregation Principle)
19interface Validator {
20 validate(data: any): ValidationResult;
21}
22
23// Individual validators with single responsibilities (Single Responsibility Principle)
24class NameValidator implements Validator {
25 validate(name: string): ValidationResult {
26 if (name.length < 2) {
27 return { isValid: false, error: "Name is too short" };
28 }
29 return { isValid: true };
30 }
31}
32
33class EmailValidator implements Validator {
34 validate(email: string): ValidationResult {
35 if (!email.includes("@")) {
36 return { isValid: false, error: "Invalid email format" };
37 }
38 return { isValid: true };
39 }
40}
41
42class AgeValidator implements Validator {
43 validate(age: number | undefined): ValidationResult {
44 if (age !== undefined && age < 18) {
45 return { isValid: false, error: "User must be 18 or older" };
46 }
47 return { isValid: true };
48 }
49}
50
51// Composite validator that uses individual validators (Open/Closed Principle)
52class UserValidator {
53 private validators: { [key: string]: Validator } = {
54 name: new NameValidator(),
55 email: new EmailValidator(),
56 age: new AgeValidator(),
57 };
58
59 validate(user: UserData): ValidationResult {
60 // Validate name
61 const nameResult = this.validators.name.validate(user.name);
62 if (!nameResult.isValid) {
63 return nameResult;
64 }
65
66 // Validate email
67 const emailResult = this.validators.email.validate(user.email);
68 if (!emailResult.isValid) {
69 return emailResult;
70 }
71
72 // Validate age
73 const ageResult = this.validators.age.validate(user.age);
74 if (!ageResult.isValid) {
75 return ageResult;
76 }
77
78 // All validations passed
79 return { isValid: true };
80 }
81}
82
83// Example usage
84const user = {
85 name: "Jo",
86 email: "jo.example.com",
87 age: 25,
88 isActive: true,
89};
90
91const userValidator = new UserValidator();
92const result = userValidator.validate(user);
93
94if (result.isValid) {
95 console.log("User is valid");
96} else {
97 console.log("Error: " + result.error);
98}
99
100export {
101 AgeValidator,
102 EmailValidator,
103 NameValidator,
104 UserValidator,
105 ValidationResult,
106 Validator,
107};
108
109/*
110Code Smells:
111Using global state (validationError variable)
112Single validation function doing multiple things
113No clear error handling mechanism
114Tightly coupled validation logic
115
116Fix:
1171. Single Responsibility Principle (SRP)
118Created separate validator classes for each validation concern
119Each validator is responsible for only one type of validation (name, email, age)
120
121
1222. Open/Closed Principle (OCP)
123The validation system is open for extension (you can add new validators)
124The core UserValidator class doesn't need to change when adding new validation rules
125
126
1273. Interface Segregation Principle (ISP)
128Created a clean Validator interface that all validators implement
129Each validator only needs to implement a single validation method
130
131
1324. Dependency Inversion Principle (DIP)
133The UserValidator depends on abstractions (the Validator interface)
134Specific validators are injected into the UserValidator
135
136
1375. Other improvements:
138Eliminated global state by using return values instead of global variables
139Created a proper ValidationResult interface for structured error reporting
140Made the validation logic more modular and testable
141Each class and function has a clear, single responsibility
142*/