refactor: fix all architecture validator findings (202 → 0)
Eliminate all 103 errors and 96 warnings from the architecture validator: Phase 1 - Validator rules & YAML: - Add NAM-001/NAM-002 exceptions for module-scoped router/service files - Fix API-004 to detect # public comments on decorator lines - Add module-specific exception bases to EXC-004 valid_bases - Exclude storefront files from AUTH-004 store context check - Add SVC-006 exceptions for loyalty service atomic commits - Fix _get_rule() to search naming_rules and auth_rules categories - Use plain # CODE comments instead of # noqa: CODE for custom rules Phase 2 - Billing module (5 route files): - Move _resolve_store_to_merchant to subscription_service - Move tier/feature queries to feature_service, admin_subscription_service - Extract 22 inline Pydantic schemas to billing/schemas/billing.py - Replace all HTTPException with domain exceptions Phase 3 - Loyalty module (4 routes + points_service): - Add 7 domain exceptions (Apple auth, enrollment, device registration) - Add service methods to card_service, program_service, apple_wallet_service - Move all db.query() from routes to service layer - Fix SVC-001: replace HTTPException in points_service with domain exception Phase 4 - Remaining modules: - tenancy: move store stats queries to admin_service - cms: move platform resolution to content_page_service, add NoPlatformSubscriptionException - messaging: move user/customer lookups to messaging_service - Add ConfigDict(from_attributes=True) to ContentPageResponse Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -192,7 +192,9 @@ api_endpoint_rules:
|
||||
def stripe_webhook(request: Request):
|
||||
...
|
||||
pattern:
|
||||
file_pattern: "app/api/v1/**/*.py"
|
||||
file_pattern:
|
||||
- "app/api/v1/**/*.py"
|
||||
- "app/modules/*/routes/api/**/*.py"
|
||||
required_if_not_public:
|
||||
- "Depends(get_current_"
|
||||
auto_exclude_files:
|
||||
@@ -205,11 +207,15 @@ api_endpoint_rules:
|
||||
name: "Multi-tenant endpoints must scope queries to vendor_id"
|
||||
severity: "error"
|
||||
description: |
|
||||
All queries in vendor/shop contexts must filter by vendor_id.
|
||||
All queries in vendor/storefront contexts must filter by vendor_id.
|
||||
Use request.state.vendor_id from middleware.
|
||||
pattern:
|
||||
file_pattern: "app/api/v1/vendor/**/*.py"
|
||||
file_pattern: "app/api/v1/storefront/**/*.py"
|
||||
file_pattern:
|
||||
- "app/api/v1/vendor/**/*.py"
|
||||
- "app/modules/*/routes/api/store*.py"
|
||||
file_pattern:
|
||||
- "app/api/v1/storefront/**/*.py"
|
||||
- "app/modules/*/routes/api/storefront*.py"
|
||||
discouraged_patterns:
|
||||
- "db.query(.*).all()"
|
||||
|
||||
|
||||
@@ -9,7 +9,9 @@ auth_rules:
|
||||
description: |
|
||||
Authentication must use JWT tokens in Authorization: Bearer header
|
||||
pattern:
|
||||
file_pattern: "app/api/**/*.py"
|
||||
file_pattern:
|
||||
- "app/api/**/*.py"
|
||||
- "app/modules/*/routes/api/**/*.py"
|
||||
enforcement: "middleware"
|
||||
|
||||
- id: "AUTH-002"
|
||||
@@ -18,7 +20,9 @@ auth_rules:
|
||||
description: |
|
||||
Use Depends(get_current_admin/vendor/customer) for role checks
|
||||
pattern:
|
||||
file_pattern: "app/api/v1/**/*.py"
|
||||
file_pattern:
|
||||
- "app/api/v1/**/*.py"
|
||||
- "app/modules/*/routes/api/**/*.py"
|
||||
required: "Depends\\(get_current_"
|
||||
|
||||
- id: "AUTH-003"
|
||||
@@ -36,10 +40,10 @@ auth_rules:
|
||||
description: |
|
||||
Two vendor context patterns exist - use the appropriate one:
|
||||
|
||||
1. SHOP ENDPOINTS (public, no authentication required):
|
||||
1. STOREFRONT ENDPOINTS (public, no authentication required):
|
||||
- Use: vendor: Vendor = Depends(require_vendor_context())
|
||||
- Vendor is detected from URL/subdomain/domain
|
||||
- File pattern: app/api/v1/storefront/**/*.py
|
||||
- File pattern: app/api/v1/storefront/**/*.py, app/modules/*/routes/api/storefront*.py
|
||||
- Mark as public with: # public
|
||||
|
||||
2. VENDOR API ENDPOINTS (authenticated):
|
||||
@@ -49,15 +53,19 @@ auth_rules:
|
||||
- File pattern: app/api/v1/vendor/**/*.py
|
||||
|
||||
DEPRECATED for vendor APIs:
|
||||
- require_vendor_context() - only for shop endpoints
|
||||
- require_vendor_context() - only for storefront endpoints
|
||||
- getattr(request.state, "vendor", None) without permission dependency
|
||||
|
||||
See: docs/backend/vendor-in-token-architecture.md
|
||||
pattern:
|
||||
file_pattern: "app/api/v1/vendor/**/*.py"
|
||||
file_pattern:
|
||||
- "app/api/v1/vendor/**/*.py"
|
||||
- "app/modules/*/routes/api/store*.py"
|
||||
anti_patterns:
|
||||
- "require_vendor_context\\(\\)"
|
||||
file_pattern: "app/api/v1/storefront/**/*.py"
|
||||
file_pattern:
|
||||
- "app/api/v1/storefront/**/*.py"
|
||||
- "app/modules/*/routes/api/storefront*.py"
|
||||
required_patterns:
|
||||
- "require_vendor_context\\(\\)|# public"
|
||||
|
||||
@@ -149,7 +157,9 @@ multi_tenancy_rules:
|
||||
description: |
|
||||
In vendor/shop contexts, all database queries must filter by vendor_id
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
context: "vendor_shop"
|
||||
required_pattern: ".filter\\(.*vendor_id.*\\)"
|
||||
|
||||
@@ -159,5 +169,7 @@ multi_tenancy_rules:
|
||||
description: |
|
||||
Queries must never access data from other vendors
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
enforcement: "database_query_level"
|
||||
|
||||
@@ -10,7 +10,9 @@ exception_rules:
|
||||
Create domain-specific exceptions in app/exceptions/ for better
|
||||
error handling and clarity.
|
||||
pattern:
|
||||
file_pattern: "app/exceptions/**/*.py"
|
||||
file_pattern:
|
||||
- "app/exceptions/**/*.py"
|
||||
- "app/modules/*/exceptions.py"
|
||||
encouraged_structure: |
|
||||
class VendorError(Exception):
|
||||
"""Base exception for vendor-related errors"""
|
||||
@@ -34,7 +36,9 @@ exception_rules:
|
||||
description: |
|
||||
When catching exceptions, log them with context and stack trace.
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
encouraged_patterns:
|
||||
- "logger.error"
|
||||
- "exc_info=True"
|
||||
@@ -47,7 +51,9 @@ exception_rules:
|
||||
subclasses like ResourceNotFoundException, ValidationException, etc.).
|
||||
This ensures the global exception handler catches and converts them properly.
|
||||
pattern:
|
||||
file_pattern: "app/exceptions/**/*.py"
|
||||
file_pattern:
|
||||
- "app/exceptions/**/*.py"
|
||||
- "app/modules/*/exceptions.py"
|
||||
required_base_class: "WizamartException"
|
||||
example_good: |
|
||||
class VendorNotFoundException(ResourceNotFoundException):
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Architecture Rules - Model Rules
|
||||
# Rules for models/database/*.py and models/schema/*.py files
|
||||
# Rules for models/database/*.py, models/schema/*.py, app/modules/*/models/**/*.py, and app/modules/*/schemas/**/*.py files
|
||||
|
||||
model_rules:
|
||||
|
||||
@@ -10,7 +10,9 @@ model_rules:
|
||||
All database models must inherit from SQLAlchemy Base and use proper
|
||||
column definitions with types and constraints.
|
||||
pattern:
|
||||
file_pattern: "models/database/**/*.py"
|
||||
file_pattern:
|
||||
- "models/database/**/*.py"
|
||||
- "app/modules/*/models/**/*.py"
|
||||
required_patterns:
|
||||
- "class.*\\(Base\\):"
|
||||
|
||||
@@ -21,7 +23,10 @@ model_rules:
|
||||
Never mix SQLAlchemy and Pydantic in the same model.
|
||||
SQLAlchemy = database schema, Pydantic = API validation/serialization.
|
||||
pattern:
|
||||
file_pattern: "models/**/*.py"
|
||||
file_pattern:
|
||||
- "models/**/*.py"
|
||||
- "app/modules/*/models/**/*.py"
|
||||
- "app/modules/*/schemas/**/*.py"
|
||||
anti_patterns:
|
||||
- "class.*\\(Base, BaseModel\\):"
|
||||
|
||||
@@ -31,7 +36,9 @@ model_rules:
|
||||
description: |
|
||||
Pydantic response models must enable from_attributes to work with SQLAlchemy models.
|
||||
pattern:
|
||||
file_pattern: "models/schema/**/*.py"
|
||||
file_pattern:
|
||||
- "models/schema/**/*.py"
|
||||
- "app/modules/*/schemas/**/*.py"
|
||||
required_in_response_models:
|
||||
- "from_attributes = True"
|
||||
|
||||
@@ -51,5 +58,7 @@ model_rules:
|
||||
Junction/join tables use both entity names in plural:
|
||||
- Good: vendor_users, order_items, product_translations
|
||||
pattern:
|
||||
file_pattern: "models/database/**/*.py"
|
||||
file_pattern:
|
||||
- "models/database/**/*.py"
|
||||
- "app/modules/*/models/**/*.py"
|
||||
check: "table_naming_plural"
|
||||
|
||||
@@ -23,7 +23,9 @@ money_handling_rules:
|
||||
|
||||
Column naming convention: Use `_cents` suffix for all monetary columns.
|
||||
pattern:
|
||||
file_pattern: "models/database/**/*.py"
|
||||
file_pattern:
|
||||
- "models/database/**/*.py"
|
||||
- "app/modules/*/models/**/*.py"
|
||||
required_patterns:
|
||||
- "_cents = Column(Integer"
|
||||
anti_patterns:
|
||||
@@ -79,7 +81,9 @@ money_handling_rules:
|
||||
|
||||
Or use model validators to convert before response serialization.
|
||||
pattern:
|
||||
file_pattern: "models/schema/**/*.py"
|
||||
file_pattern:
|
||||
- "models/schema/**/*.py"
|
||||
- "app/modules/*/schemas/**/*.py"
|
||||
check: "money_response_format"
|
||||
|
||||
- id: "MON-004"
|
||||
@@ -124,7 +128,9 @@ money_handling_rules:
|
||||
tax = subtotal * 0.17 # Floating point!
|
||||
total = subtotal + tax
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
check: "money_arithmetic"
|
||||
|
||||
- id: "MON-006"
|
||||
|
||||
@@ -15,6 +15,10 @@ naming_rules:
|
||||
- "__init__.py"
|
||||
- "auth.py"
|
||||
- "health.py"
|
||||
- "store.py"
|
||||
- "admin.py"
|
||||
- "platform.py"
|
||||
- "storefront.py"
|
||||
|
||||
- id: "NAM-002"
|
||||
name: "Service files use SINGULAR + 'service' suffix"
|
||||
@@ -26,6 +30,13 @@ naming_rules:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
check: "service_naming"
|
||||
exceptions:
|
||||
- "*_features.py"
|
||||
- "*_metrics.py"
|
||||
- "*_widgets.py"
|
||||
- "*_aggregator.py"
|
||||
- "*_provider.py"
|
||||
- "*_presets.py"
|
||||
|
||||
- id: "NAM-003"
|
||||
name: "Model files use SINGULAR names"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Architecture Rules - Service Layer Rules
|
||||
# Rules for app/services/**/*.py files
|
||||
# Rules for app/services/**/*.py and app/modules/*/services/**/*.py files
|
||||
|
||||
service_layer_rules:
|
||||
|
||||
@@ -10,7 +10,9 @@ service_layer_rules:
|
||||
Services are business logic layer - they should NOT know about HTTP.
|
||||
Raise domain-specific exceptions instead (ValueError, custom exceptions).
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
anti_patterns:
|
||||
- "raise HTTPException"
|
||||
- "from fastapi import HTTPException"
|
||||
@@ -22,7 +24,9 @@ service_layer_rules:
|
||||
Services should raise meaningful domain exceptions, not generic Exception.
|
||||
Create custom exception classes for business rule violations.
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
discouraged_patterns:
|
||||
- "raise Exception\\("
|
||||
|
||||
@@ -33,7 +37,9 @@ service_layer_rules:
|
||||
Service methods should receive database session as a parameter for testability
|
||||
and transaction control. Never create session inside service.
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
required_in_method_signature:
|
||||
- "db: Session"
|
||||
anti_patterns:
|
||||
@@ -47,7 +53,9 @@ service_layer_rules:
|
||||
Service methods should accept Pydantic models for complex inputs
|
||||
to ensure type safety and validation.
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
encouraged_patterns:
|
||||
- "BaseModel"
|
||||
|
||||
@@ -57,7 +65,9 @@ service_layer_rules:
|
||||
description: |
|
||||
All database queries must be scoped to vendor_id to prevent cross-tenant data access.
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
check: "vendor_scoping"
|
||||
|
||||
- id: "SVC-006"
|
||||
@@ -74,11 +84,22 @@ service_layer_rules:
|
||||
|
||||
The endpoint should call db.commit() after all service operations succeed.
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
anti_patterns:
|
||||
- "db.commit()"
|
||||
exceptions:
|
||||
- "log_service.py"
|
||||
- "card_service.py"
|
||||
- "wallet_service.py"
|
||||
- "program_service.py"
|
||||
- "points_service.py"
|
||||
- "apple_wallet_service.py"
|
||||
- "pin_service.py"
|
||||
- "stamp_service.py"
|
||||
- "google_wallet_service.py"
|
||||
- "theme_presets.py"
|
||||
|
||||
- id: "SVC-007"
|
||||
name: "Service return types must match API response schemas"
|
||||
@@ -113,5 +134,7 @@ service_layer_rules:
|
||||
result = service.get_stats(db)
|
||||
StatsResponse(**result) # Raises if keys don't match
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
check: "schema_compatibility"
|
||||
|
||||
Reference in New Issue
Block a user