mirror of
https://github.com/samiyev/puaros.git
synced 2025-12-28 07:16:53 +05:00
feat: add repository pattern validation (v0.5.0)
Add comprehensive Repository Pattern validation to detect violations and ensure proper domain-infrastructure separation. Features: - ORM type detection in repository interfaces (25+ patterns) - Concrete repository usage detection in use cases - Repository instantiation detection (new Repository()) - Domain language validation for repository methods - Smart violation reporting with fix suggestions Tests: - 31 new tests for repository pattern detection - 292 total tests passing (100% pass rate) - 96.77% statement coverage, 83.82% branch coverage Examples: - 8 example files (4 bad patterns, 4 good patterns) - Demonstrates Clean Architecture and SOLID principles
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* ✅ GOOD EXAMPLE: Repository interface in use case
|
||||
*
|
||||
* Use case depends on repository interface, not concrete implementation.
|
||||
* This follows Dependency Inversion Principle.
|
||||
*/
|
||||
|
||||
class CreateUser {
|
||||
constructor(private readonly userRepo: IUserRepository) {}
|
||||
|
||||
async execute(data: CreateUserRequest): Promise<UserResponseDto> {
|
||||
const user = User.create(Email.from(data.email), data.name)
|
||||
await this.userRepo.save(user)
|
||||
return UserMapper.toDto(user)
|
||||
}
|
||||
}
|
||||
|
||||
interface IUserRepository {
|
||||
save(user: User): Promise<void>
|
||||
findByEmail(email: Email): Promise<User | null>
|
||||
}
|
||||
|
||||
class Email {
|
||||
private constructor(private readonly value: string) {}
|
||||
|
||||
static from(value: string): Email {
|
||||
if (!value.includes("@")) {
|
||||
throw new Error("Invalid email")
|
||||
}
|
||||
return new Email(value)
|
||||
}
|
||||
|
||||
getValue(): string {
|
||||
return this.value
|
||||
}
|
||||
}
|
||||
|
||||
class User {
|
||||
static create(email: Email, name: string): User {
|
||||
return new User(email, name)
|
||||
}
|
||||
|
||||
private constructor(
|
||||
private readonly email: Email,
|
||||
private readonly name: string,
|
||||
) {}
|
||||
|
||||
getEmail(): Email {
|
||||
return this.email
|
||||
}
|
||||
|
||||
getName(): string {
|
||||
return this.name
|
||||
}
|
||||
}
|
||||
|
||||
interface CreateUserRequest {
|
||||
email: string
|
||||
name: string
|
||||
}
|
||||
|
||||
interface UserResponseDto {
|
||||
email: string
|
||||
name: string
|
||||
}
|
||||
|
||||
class UserMapper {
|
||||
static toDto(user: User): UserResponseDto {
|
||||
return {
|
||||
email: user.getEmail().getValue(),
|
||||
name: user.getName(),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user