diff --git a/packages/guardian/examples/bad-architecture/framework-leaks/UserWithFrameworks.ts b/packages/guardian/examples/bad-architecture/framework-leaks/UserWithFrameworks.ts new file mode 100644 index 0000000..685fcb9 --- /dev/null +++ b/packages/guardian/examples/bad-architecture/framework-leaks/UserWithFrameworks.ts @@ -0,0 +1,23 @@ +/** + * BAD EXAMPLE: Framework Leaks in Domain Layer + * This file should be in a domain layer structure to be detected + */ + +import { PrismaClient } from "@prisma/client" +import { Request, Response } from "express" +import axios from "axios" + +export class UserWithFrameworkLeaks { + private prisma = new PrismaClient() + + async save(): Promise { + await this.prisma.user.create({ + data: { id: "1", email: "test@example.com" }, + }) + } + + async validateEmail(email: string): Promise { + const response = await axios.post("https://api.validator.com/email", { email }) + return response.data.valid + } +} diff --git a/packages/guardian/examples/bad/FrameworkLeak.ts b/packages/guardian/examples/bad/FrameworkLeak.ts new file mode 100644 index 0000000..4eb3971 --- /dev/null +++ b/packages/guardian/examples/bad/FrameworkLeak.ts @@ -0,0 +1,58 @@ +/** + * BAD EXAMPLE: Framework Leak in Domain Layer + * + * This file violates Clean Architecture by importing framework-specific packages + * directly into the domain layer, creating tight coupling. + * + * Issues: + * 1. Direct Prisma dependency in domain (ORM leak) + * 2. Express types in domain (web framework leak) + * 3. Axios in domain (HTTP client leak) + * + * Fix: Use interfaces in domain, implement in infrastructure + */ + +import { PrismaClient } from "@prisma/client" +import { Request, Response } from "express" +import axios from "axios" + +// ❌ Bad: Domain entity with database dependency +export class UserWithPrisma { + private prisma = new PrismaClient() + + constructor( + private id: string, + private email: string, + ) {} + + async save(): Promise { + await this.prisma.user.create({ + data: { id: this.id, email: this.email }, + }) + } + + async find(id: string): Promise { + const user = await this.prisma.user.findUnique({ where: { id } }) + return user ? new UserWithPrisma(user.id, user.email) : null + } +} + +// ❌ Bad: Domain service with HTTP dependency +export class UserServiceWithAxios { + async validateEmail(email: string): Promise { + const response = await axios.post("https://api.validator.com/email", { email }) + return response.data.valid + } +} + +// ❌ Bad: Domain value object with web framework dependency +export class EmailRequest { + constructor( + public req: Request, + public res: Response, + ) {} + + getEmail(): string { + return this.req.body.email + } +}