| 1 | // A refactored user validation utility following SOLID principles |
| 2 | // Solution for next-edit-2-1.ts |
| 3 | |
| 4 | type UserData = { |
| 5 | id?: number; |
| 6 | name: string; |
| 7 | email: string; |
| 8 | age?: number; |
| 9 | isActive: boolean; |
| 10 | }; |
| 11 | |
| 12 | // Define a validation result interface |
| 13 | interface ValidationResult { |
| 14 | isValid: boolean; |
| 15 | error?: string; |
| 16 | } |
| 17 | |
| 18 | // Create a validator interface (Interface Segregation Principle) |
| 19 | interface Validator { |
| 20 | validate(data: any): ValidationResult; |
| 21 | } |
| 22 | |
| 23 | // Individual validators with single responsibilities (Single Responsibility Principle) |
| 24 | class 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 | |
| 33 | class 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 | |
| 42 | class 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) |
| 52 | class 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 |
| 84 | const user = { |
| 85 | name: "Jo", |
| 86 | email: "jo.example.com", |
| 87 | age: 25, |
| 88 | isActive: true, |
| 89 | }; |
| 90 | |
| 91 | const userValidator = new UserValidator(); |
| 92 | const result = userValidator.validate(user); |
| 93 | |
| 94 | if (result.isValid) { |
| 95 | console.log("User is valid"); |
| 96 | } else { |
| 97 | console.log("Error: " + result.error); |
| 98 | } |
| 99 | |
| 100 | export { |
| 101 | AgeValidator, |
| 102 | EmailValidator, |
| 103 | NameValidator, |
| 104 | UserValidator, |
| 105 | ValidationResult, |
| 106 | Validator, |
| 107 | }; |
| 108 | |
| 109 | /* |
| 110 | Code Smells: |
| 111 | Using global state (validationError variable) |
| 112 | Single validation function doing multiple things |
| 113 | No clear error handling mechanism |
| 114 | Tightly coupled validation logic |
| 115 | |
| 116 | Fix: |
| 117 | 1. Single Responsibility Principle (SRP) |
| 118 | Created separate validator classes for each validation concern |
| 119 | Each validator is responsible for only one type of validation (name, email, age) |
| 120 | |
| 121 | |
| 122 | 2. Open/Closed Principle (OCP) |
| 123 | The validation system is open for extension (you can add new validators) |
| 124 | The core UserValidator class doesn't need to change when adding new validation rules |
| 125 | |
| 126 | |
| 127 | 3. Interface Segregation Principle (ISP) |
| 128 | Created a clean Validator interface that all validators implement |
| 129 | Each validator only needs to implement a single validation method |
| 130 | |
| 131 | |
| 132 | 4. Dependency Inversion Principle (DIP) |
| 133 | The UserValidator depends on abstractions (the Validator interface) |
| 134 | Specific validators are injected into the UserValidator |
| 135 | |
| 136 | |
| 137 | 5. Other improvements: |
| 138 | Eliminated global state by using return values instead of global variables |
| 139 | Created a proper ValidationResult interface for structured error reporting |
| 140 | Made the validation logic more modular and testable |
| 141 | Each class and function has a clear, single responsibility |
| 142 | */ |